So the syntax is as follows: string.charCodeAt(index).
If the index is not specified, it will have a default value of 0.
Recall that Unicode is an encoding that assigns a unique number to each alphanumeric character. This encoding is independent of the language used.
For more information, see the following link: unicode encoding.
In this first example we will get the Unicode of the first character of a string.
var sentence = 'do coding creativo'; var character = sentence.charCodeAt(0); console.log(character);
So, in the browser console, we get the value 102, which is the corresponding Unicode character of the letter f.
In this example we get the Unicode encoding of the last character of a string taken as input.
To do this, it is necessary to use the length property on the string which, remember, returns the number of characters of a given string.
Here is the complete example:
var sentence = 'do coding creativo'; var character = sentence.charCodeAt(sentence.length-1); console.log(character);
In this case, the value 101 is obtained, which corresponds to the Unicode encoding of the letter o.
If we try to obtain the Unicode encoding of an index that does not exist then we will have the value NaN (Not a Number).