How do I get the length of a string in JavaScript?

To get the length of a string in JavaScript, you can to use the string.length property. The length property specifies how many characters the string contains (the length of an empty string is 0). When determining the length of a string, keep in mind that spaces and various characters are also part of the string; each one is a separate character. While the length property is commonly used with other methods in JavaScript, it's important to remember that length itself is not a method and is not calculated every time (so it's fast). In this example, we get the length of a JavaScript string using the length property. Click Execute to run the JavaScript String Length Example online and see the result.
Loading...