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 property returns the number of code units in the string.
UTF-16
, the string format used by JavaScript, uses a single 16-bit code unit to represent the most common characters, but needs to use two code units for less commonly-used characters, so it's possible for the value returned by
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.
Since `length` counts code units instead of characters, if you want to get the number of characters you need something like this:
function getCharacterLength (str) {
// The string iterator that is used here iterates over characters,
// not mere code units
return [...str].length;
}
console.log(getCharacterLength('A\uD87E\uDC04Z')); // 3
// While not recommended, you could add this to each string as follows:
Object.defineProperty(String.prototype, 'charLength', {
get () {
return getCharacterLength(this);
}
});
console.log('A\uD87E\uDC04Z'.charLength); // 3
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"
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 (ECMA-262) |
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.
更新 GitHub 上的兼容性数据| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
length
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
完整支持
String
String 长度
String.fromCharCode()
String.fromCodePoint()
String.prototype.anchor()
String.prototype.big()
String.prototype.blink()
String.prototype.bold()
String.prototype.charAt()
String.prototype.charCodeAt()
String.prototype.codePointAt()
String.prototype.concat()
String.prototype.endsWith()
String.prototype.fixed()
String.prototype.fontcolor()
String.prototype.fontsize()
String.prototype.includes()
String.prototype.indexOf()
String.prototype.italics()
String.prototype.lastIndexOf()
String.prototype.link()
String.prototype.localeCompare()
String.prototype.match()
String.prototype.matchAll()
String.prototype.normalize()
String.prototype.padEnd()
String.prototype.padStart()
String.prototype.repeat()
String.prototype.replace()
String.prototype.replaceAll()
String.prototype.search()
String.prototype.slice()
String.prototype.small()
String.prototype.split()
String.prototype.startsWith()
String.prototype.strike()
String.prototype.sub()
String.prototype.substr()
String.prototype.substring()
String.prototype.sup()
String.prototype.toLocaleLowerCase()
String.prototype.toLocaleUpperCase()
String.prototype.toLowerCase()
String.prototype.toSource()
String.prototype.toString()
String.prototype.toUpperCase()
String.prototype.trim()
String.prototype.trimEnd()
String.prototype.trimStart()
String.prototype.valueOf()
String.prototype[@@iterator]()
String.raw()
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()