Getting String Length 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.
Getting String Length in JavaScript Execute
let str = 'String Length Example';

console.log(str.length);
Updated: Viewed: 5735 times

JavaScript string length property

Following is the syntax of the string.length property:

JavaScript String Length Syntax
string.length

Where:
  • string: the string whose length you want to return
JavaScript String Length Example
let str = 'JavaScript String Length';

console.log(str.length);

// output: 24

See also