0

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?

Nitish Jain
  • 704
  • 2
  • 10
  • 24

2 Answers2

3

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thanks, so that means that JavaScript string will require more memory than the string in the C language, right? – Nitish Jain Aug 25 '15 at 04:34
  • 1
    @jain: Yes, typically. Though there's nothing stopping engines from using a more memory-efficient representation, at the expense of more complicated indexed access. But notice that C doesn't really have strings, it has `char[]`, and you could do the same in JS with a `Uint8Array`. Also, C is obviously more limited at storing Unicode characters (though even JS is still limited, and needs to use surrogate pairs to represent code points outside of the BMP) – Bergi Aug 25 '15 at 10:41
0

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

Jack A.
  • 4,245
  • 1
  • 20
  • 34