| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/length | [Back] [Original] |
Get to know MDN better
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
(UTF-8 blob) JavaScript UTF-16
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
function getCharacterLength(str) {
//
//
return [...str].length;
}
console.log(getCharacterLength("A\uD87E\uDC04Z")); // 3
Intl.Segmenter segment() Segments
function getGraphemeCount(str) {
const segmenter = new Intl.Segmenter("ja-JP", { granularity: "grapheme" });
// Segments
// Unicode
return [...segmenter.segment(str)].length;
}
console.log(getGraphemeCount("")); // 1
const x = "Mozilla";
const empty = "";
console.log(`${x} ${x.length} `);
/* "Mozilla 7 " */
console.log(` ${empty.length} `);
/* " 0 " */
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
const myString = "bluebells";
myString.length = 4;
console.log(myString); // "bluebells"
console.log(myString.length); // 9
| ECMAScript 2027 LanguageSpecification # sec-properties-of-string-instances-length |
Stringanchor()at()big()blink()bold()charAt()charCodeAt()codePointAt()concat()endsWith()fixed()fontcolor()fontsize()includes()indexOf()isWellFormed()italics()lastIndexOf()link()localeCompare()match()matchAll()normalize()padEnd()padStart()repeat()replace()replaceAll()search()slice()small()split()startsWith()strike()sub()substr()substring()sup()toLocaleLowerCase()toLocaleUpperCase()toLowerCase()toString()toUpperCase()toWellFormed()trim()trimEnd()trimStart()valueOf()[Symbol.iterator]()Object/Function| Web Proxy Viewer | New URL | Original Page |