--- title: String.length slug: Web/JavaScript/Reference/Global_Objects/String/length tags: - JavaScript - Property - Prototype - String translation_of: Web/JavaScript/Reference/Global_Objects/String/length ---
{{JSRef("Global_Objects", "String")}}
length 属性表示一个字符串的长度。
该属性返回字符串中字符编码单元的数量。JavaScript 使用 {{ interwiki("wikipedia", "UTF-16") }} 编码,该编码使用一个 16 比特的编码单元来表示大部分常见的字符,使用两个代码单元表示不常用的字符。因此 length 返回值可能与字符串中实际的字符数量不相同。
空字符串的 length 为 0。
静态属性 String.length 返回 1。
var x = "Mozilla";
var empty = "";
console.log("Mozilla is " + x.length + " code units long");
/* "Mozilla is 7 code units long" */
console.log("The empty string is has a length of " + empty.length);
/* "The empty string is has a length of 0" */
| Specification | Status | Comment |
|---|---|---|
| {{SpecName('ES1')}} | {{Spec2('ES1')}} | Initial definition. Implemented in JavaScript 1.0. |
| {{SpecName('ES5.1', '#sec-15.5.5.1', 'String.prototype.length')}} | {{Spec2('ES5.1')}} | |
| {{SpecName('ES6', '#sec-properties-of-string-instances-length', 'String.prototype.length')}} | {{Spec2('ES6')}} |