5/6/2023 0 Comments Using codepoints javascript![]() ![]() Well, that’s where Unicode and its UTF-16 encoding scheme comes into the game. That’s clear.īut this doesn’t really tell us how does JavaScript go from a number like 97 to the character a. JavaScript strings are sequences of 16-bit code units. It might sound complicated, but at the core, it’s a very simple idea.įollowing from this idea, the length string property does NOT return the number of characters in a given string, but rather the number of these 16-bit code units. This right away clarifies the fact that a string is, technically, NOT a sequence of characters instead it’s a sequence of code units that when parsed, form a sequence of (visual) characters. Some characters such as □ consume 2 code units. a 16-bit number), however this doesn’t hold for all characters. Most characters in JavaScript do indeed consume one single code unit (i.e. The most important thing is that each element is NOT strictly a character. Moving on, each element in this sequence is a 16-bit number, generally referred to as a 16-bit code unit, or simply as a code unit. To start with, one thing is absolutely clear from this definition - that whatever the case be, a string is a sequence type. Primitive value that is a finite ordered sequence of zero or more 16-bit unsigned integer values. So, from the perspective of JavaScript, a string is defined in the following way, as per the ECMAScript spec: Right now we are concerned with JavaScript, right? It could have different interpretations with every programming language. We don’t really know what exactly is the meaning of a ‘character’ here. This definition ain’t wrong - after all, a string is actually a sequence of characters.īut if we really zoom into the depth of the memory, we see that this definition is somehow vague. That is,Ī string is a sequence of textual characters. A string in any programming language is defined in a very typical and simple way.
0 Comments
Leave a Reply. |