[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/length [Back]  [Original]

String: length - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

String: length

Baseline

20157

length String UTF-16

const str = "Life, the universe and everything. Answer:";

console.log(`${str} ${str.length}`);
// : "Life, the universe and everything. Answer: 42"

String: length

JavaScript UTF-16 Unicode 1 2 length Unicode length

253 - 1 16,384TiB 32

  • V8 Chrome node 229 - 24 (~1GiB) 32 228 - 16 (~512MiB)
  • Firefox 230 - 2 (~2GiB) Firefox 65 228 - 1 (~512MiB)
  • Safari 231 - 1 (~4GiB)

(UTF-8 blob) JavaScript UTF-16

js
const str1 = "a".repeat(2 ** 29 - 24); // 
const str2 = "a".repeat(2 ** 29 - 23); // RangeError: Invalid string length

const buffer = new Uint8Array(2 ** 29 - 24).fill("a".codePointAt(0)); //  512MiB
const str = new TextDecoder().decode(buffer); //  1GiB

length 0

String.length String 1

length

js
function getCharacterLength(str) {
  // 
  // 
  return [...str].length;
}

console.log(getCharacterLength("A\uD87E\uDC04Z")); // 3

Intl.Segmenter segment() Segments

js
function getGraphemeCount(str) {
  const segmenter = new Intl.Segmenter("ja-JP", { granularity: "grapheme" });

  //  Segments 
  //  Unicode 
  return [...segmenter.segment(str)].length;
}

console.log(getGraphemeCount("")); // 1

js
const x = "Mozilla";
const empty = "";

console.log(`${x}  ${x.length} `);
/* "Mozilla  7 " */

console.log(` ${empty.length} `);
/* " 0 " */

length

js
const emoji = "";
console.log(emoji.length); // 2
console.log([...emoji].length); // 1
const adlam = "";
console.log(adlam.length); // 8
console.log([...adlam].length); // 4
const formula = ",0";
console.log(formula.length); // 11
console.log([...formula].length); // 9

length

length

js
const myString = "bluebells";

myString.length = 4;
console.log(myString); // "bluebells"
console.log(myString.length); // 9

ECMAScript 2027 LanguageSpecification
# sec-properties-of-string-instances-length


Web Proxy Viewer  |  New URL  |  Original Page