Reputation: 734
From the MDN
JavaScript's String type is used to represent textual data. It is a set of "elements" of 16-bit unsigned integer values. Each element in the String occupies a position in the String. The first element is at index 0, the next at index 1, and so on. The length of a String is the number of elements in it. You can create strings using string literals or string objects.
How the JavaScript String type is a set of "elements" of 16-bit unsigned integer values, why not 8-bit unsigned integer values?
Upvotes: 0
Views: 1296
Reputation: 665130
Looking at the full spec text helps here:
The String type is the set of all ordered sequences of zero or more 16-bit unsigned integer values (“elements”) up to a maximum length of 253-1 elements. The String type is generally used to represent textual data in a running ECMAScript program, in which case each element in the String is treated as a UTF-16 code unit value.
Similar can be found in the ES5.1 spec.
Why not 8-bit unsigned integer values? That would have been equally possible, using UTF-8. But it wasn't done, and that's just how it is now.
Upvotes: 3
Reputation: 4453
Because EcmaScript is written using Unicode, a.k.a. UTF-16:
ECMAScript source text is assumed to be a sequence of 16-bit code units for the purposes of this specification.
http://www.ecma-international.org/ecma-262/5.1/#sec-6
Upvotes: 0