In ECMAScript 6 new methods similar to the String.fromCharCode()
and String#charCodeAt()
were added: String.fromCodePoint()
and String#codePointAt()
.
What's the difference between them?
In ECMAScript 6 new methods similar to the String.fromCharCode()
and String#charCodeAt()
were added: String.fromCodePoint()
and String#codePointAt()
.
What's the difference between them?
The difference is simple. First, a String value (more expressed as "string") stores a sequence of nonnegative integers, the size of 2 bytes (16 bits), called "elements" for easy reference.
The String#charCodeAt(índice)
method, for example, only returns the value of the element in the index (which by convention starts from 0
) specified as a number. The static method String.fromCharCode(...códigos)
returns a value of type String, where each element equals each given code.
Now, difference from String#codePointAt(índice)
. Based on UTF-16 encoding, returns the given character code in the given index (index of elements, not of code).
The static method also differs from% w / w% because of UTF-16 encoding (it creates two elements if you need surrogate pairs to represent the one-character code), but it does not give exceptions for codes between String.fromCodePoint(...códigos)
and String.fromCharCode()
should).
From the documentation:
String.fromCodePoint()
The static String.fromCodePoint () method returns a string created using the specified string of code points
String.prototype.codePointAt()
The codePointAt () method returns a nonnegative integer that is the code Unicode of the code point
Links: