String#codePointAt or String#charCodeAt?

Asked

Viewed 77 times

1

New methods similar to Ecmascript 6 have been added to String.fromCharCode() and String#charCodeAt(): String.fromCodePoint() and String#codePointAt().

What’s the difference between them?

2 answers

0

0

The difference is simple. First, a value of type String (most expressed as "string") stores a sequence of non-negative integer numbers, the size of 2 bytes (16 bits), called "elements" to facilitate reference.

The method String#charCodeAt(índice), for example, only returns the value of the element in index (which by convention initiates the 0) specified as a number. The static method String.fromCharCode(...códigos) returns a String type value, where each element equals each given code.

Now, the difference of String#codePointAt(índice). Based on UTF-16 encoding, returns the character code in the index (index of elements, not code) given.

The static method String.fromCodePoint(...códigos) also differentiates from String.fromCharCode() UTF-16 coding (creates two elements if you need to surrogate pairs to represent the code of a character), but finally, does not give exceptions for codes between 0xD800 and 0xDFFF (surely you should).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.