How is JavaScript string a set of elements of integer values?

Viewed 48

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.

What does it mean when you say the JavaScript String type is a set of "elements" of 16-bit unsigned integer values? Why integer values?

4 Answers

The 16-bit unsigned integer values is a representation of specific characters and since it is a set of elements, you are able to grab specific characters within a string with [] notation as you would a list. Ex:

const string = 'john doe';
console.log(string[3]) // Will print 'n' as it is the 3rd index characters (starts at 0)

it just means that a string is an "array-like" object with each character available in a similar manner to an array element. Each of those characters are stored as a UTF-16 value.

// The following is one string literal:
let s = "ABCDEFG";

console.log(s);

// But it's also an array-like object in that it has a length and can be indexed
console.log("The length of the string is: ", s.length);
console.log("The 3rd character is: ", s[2]);

// And we can see that the characters are stored as separate UTF-16 values:
console.log(s.charCodeAt(2));

As I understood:

  1. unsigned means not + or -.
  2. 16 bit means 2^16 number of elements/characters can represent.
  3. set of Integers mean to represent a String use multiple integers (1 or more).

Therefore this means to represents a string js use set of numbers (each numbers is a 1 of 2^16 numbers because no float numbers and no positive/negative representation).

Note: to understand more read about utf-16

Reference:https://www.ibm.com/docs/en/i/7.2?topic=unicode-utf-16

In Unicode, each symbol has an associated number. For example, "A" is 65, "a" is 97, etc. These numbers are called code points. Depending on the encoding we’re using (UTF-32, UTF-16, UTF-8, ASCII etc.), we represent/encode these code points in different ways. The things we use to encode these code point numbers are called "code units", or as MDN calls them, "elements".

As we're using JavaScript, we're interested in the UTF-16 encoding of characters. This means that to represent a single code unit/"element", we use 16 bits (2 bytes). For "A", the "element" representation is:

0000000001000001 // (16 bits, hence 0 padding)

There are a lot of characters that we need to represent (think emojis, Chinese, Japanese, Korean scripts etc. that each have their own code points), so 16 bits to represent and encode all of these characters alone isn't enough. That's why sometimes some code points are encoded using two code units/elements. For example, has a code point of 128514 and in UTF16 is encoded by two elements/code units:

1101100000111101 1101111000000010

So these two code units/elements 1101100000111101 (decimal 55357) and 1101111000000010 (decimal 56834) encode the code point/"character" of 128514 which represents . Notice how both code units are both positive (unsigned), and are whole numbers (integers). UTF16 outlines the algorithm to take these elements from the element form to their code point form and vice-versa (see here for examples).

What are the implications of all this? Well it means that strings like "" will have a length of 2:

console.log("".length); // 2

And that when you access the indexes of the string, you will access the code units/"elements" of that string:

// "" in UTF16 is "1101100000111101 1101111000000010"
// So ""[0] gives 1101100000111101 (in decimal 55357)
// So ""[1] gives 1101111000000010 (in decimal 56834)
console.log(""[0], "".charCodeAt(0)); // 1101100000111101
console.log(""[1], "".charCodeAt(1)); // 1101111000000010

Related