String Character Length: Difference between revisions

Content deleted Content added
Line 183: Line 183:
[[Category:JavaScript]]
[[Category:JavaScript]]


JavaScript encodes strings in UTF-16, which represents each character with one or two 16-bit values. The most commonly used characters are represented by one 16-bit value, while rarer ones like some mathematical symbols are represented by two.
var s = "Hello, world!";

var len = s.length; // returns an integer
JavaScript has no built-in way to determine how many characters are in a string. However, if the string only contains commonly used characters, the number of characters will be equal to the number of 16-bit values used to represent the characters.
var str1 = "Hello, world!";
var len1 = str1.length; //13
var str2 = "\uD834\uDD2A"; //U+1D12A represented by a UTF-16 surrogate pair
var len2 = str2.length; //2


==[[JudoScript]]==
==[[JudoScript]]==