Count characters, bytes, lines, words and spaces.
.length 计的是 UTF-16 码元,emoji 与部分生僻汉字占 2 个码元。[...str].length 或 Intl.Segmenter 按字素簇切分更符合直觉。| Item | Formula / Note | Source / Standard |
|---|---|---|
| Units and points | JS length counts UTF-16 code units, surrogates count twice | RFC 2781 / ECMA-262 |
| Grapheme clusters | user-perceived characters should be counted as clusters | Unicode UAX 29 |
| Byte length | CJK characters take 3 UTF-8 bytes; ASCII takes 1 | RFC 3629 |