Home > Article > Web Front-end > What is the method to find the length of a string in JavaScript?
In JavaScript, you can use the length property of the String object to find the length of the string. This property can return the length of the string (number of characters). The length is in characters; the syntax format is "string.length".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
JavaScript strings are used to store and process text. String indexing starts at 0, which means the first character index is [0], the second is [1], and so on.
JavaScript to find the length of a string
In JavaScript, the length of a string can be read using the length property of the string. The length is in characters and this property is read-only.
Example:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <script> var txt = "Hello World!"; document.write(txt.length); </script> </body> </html>
Output:
12
[Related recommendations: javascript learning tutorial]
The above is the detailed content of What is the method to find the length of a string in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!