JavaScript String.prototype.length Property
The length property gives the number of characters in a string, counted as UTF-16 code units, so some emoji count as two.
Description
The length property of a String value contains the length of the string in UTF-16 code units. For everyday text this is the number of characters, but characters outside the Basic Multilingual Plane, such as many emoji, use two code units, and combined emoji can use many more.
To count what users see as characters, use [...str].length for code points, or Intl.Segmenter for grapheme clusters. length is read-only; assigning to it has no effect.
Because strings are immutable, length can't be used to shorten a string the way it can for arrays; use slice() instead. It's commonly used to check input, such as if (password.length < 8), and to loop over characters by index, although for...of handles emoji and other multi-unit characters correctly.
Syntax
string.lengthValue
A non-negative integer: the number of UTF-16 code units in the string.
Example
Browser Support
The following information will show you the current browser support for the String.prototype.length property. Hover over a browser icon to see the version that first introduced support for this property.
Desktop





Tablets & Mobile





