Search code examples
javascriptlistunicodefromcharcode

Get string from a list contain Unicode in JavaScript


I have a list of Unicode values [65, 66, 67] and I want the corresponding string "ABC". I looked into the documentation and found the function String.fromCharCode()that does what I need to do. The only problem is that the arguments needs to be a sequence of numbers.

So if I use String.fromCharCode([65, 66, 67]) it gives me " ".

Is there a way that allows the list to be treated as a sequence for a function?


Solution

  • You need to spread the array using ... spread Syntax.

    console.log(String.fromCharCode(...[65, 66, 67]));

    From MDN

    Spread syntax allows an iterable such as an array expression to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.