We have already talked about the Array length property, JavaScript Array length – the definitive guide, and now we are going to explain the JavaScript String length property.
The JavaScript String length, .length
, property is pretty similar to the Array length property with a few little differences. According to MDN: The length property of a String object contains the length of the string, in UTF-16 code units. length is a read-only data property of string instances.
This means that opposite of Array length, String .length
property is read-only and it can’t be used for anything besides reading the length of a String.
How to use JavaScript String length
The String .length is used in the following format:
let string = "This is a string"; console.log(string.length); //Result: // 16
As we can see in the example above .length
is used like any other property in JavaScript.
If you have any questions or anything you can find me on my Twitter, or you can read some of my other articles like JavaScript String replace method.