2

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?

2

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This is exactly what I was looking for.
Thanks Patrick for pointing that, Multiple website does mention the use of Spread Operator. However, MDN does mention it as Syntax, i will check the link and the specification.
2

Map on the list and then join:

var s = [65, 66, 67].map(x => String.fromCharCode(x)).join("");
console.log(s);

Comments

1

/* You can map over the list and the value you need will be output to anoter list */
var charCodes = [65, 66, 67],
    stringsFromCharCodes = charCodes.map(item => String.fromCharCode(item));

console.log('new list: ', stringsFromCharCodes);

Comments

1

you can use apply to solve this

var chars = [65,66,67]
var s = String.fromCharCode.apply({}, chars)
console.log(s);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.