Take this code:
var charCode = unkownVariable.charCodeAt(0);
What is the max length that charCode
can be? All my tests turned out as 2 character (2 digits). Will it ever be longer?
Take this code:
var charCode = unkownVariable.charCodeAt(0);
What is the max length that charCode
can be? All my tests turned out as 2 character (2 digits). Will it ever be longer?
From the specification:
Returns a Number (a nonnegative integer less than 216) representing the code unit value of the character at position
pos
in the String resulting from converting this object to a String. If there is no character at that position, the result isNaN
.
So, 216 - 1 is the maximum value, which is 65535
.
Short answer: yes.
var unkownVariable = 'test';
var charCode = unkownVariable.charCodeAt(0);
console.log(charCode); // 116
There are 16 bits used to display characters (there are different languages and charsets to consider), so the response theoretically could be anywhere between 0 and 65536 (2^16).
MDN says
Note that charCodeAt will always return a value that is less than 65,536.
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/charCodeAt