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.
length to not match the actual number of characters in the string.
ECMAScript 2016 (ed. 7) established a maximum length of
2^53 - 1 elements. Previously, no maximum length was specified. In Firefox, strings have a maximum length of
2**30 - 2 (~1GB). In versions prior to Firefox 65, the maximum length was
2**28 - 1 (~256MB).
For an empty string,
length is 0.
The static property
String.length is unrelated to the length of strings, it's the arity of the
String function (loosely, the number of formal parameters it has), which is 1.
let x = 'Mozilla'; let empty = ''; console.log(x + ' is ' + x.length + ' code units long'); /* "Mozilla is 7 code units long" */ console.log('The empty string has a length of ' + empty.length); // expected output: "The empty string has a length of 0"
Assigning to length
let myString = "bluebells"; // Attempting to assign a value to a string's .length property has no observable effect. myString.length = 4; console.log(myString); // expected output: "bluebells" console.log(myString.length); // expected output: 9
|ECMAScript Latest Draft (ECMA-262)||Draft|
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
|Chrome Full support 1||Edge Full support 12||Firefox Full support 1||IE Full support 3||Opera Full support Yes||Safari Full support Yes||WebView Android Full support 1||Chrome Android Full support 18||Firefox Android Full support 4||Opera Android Full support Yes||Safari iOS Full support Yes||Samsung Internet Android Full support 1.0||nodejs Full support Yes|
- Full support
- Full support