Numbers to chars in JavaScript and vice versa
Recently, I have to solve code problems while searching for job opportunities.
It gave me an array of numbers such as
[104,116,116,112,115,58,47,47,109,101,100,105,117,109,46,99,111,109]
and told me to find what to do.
It was not so difficult after all. But I thought it would be great to learn language concepts.
So I decided to write short posts in JavaScript, Python and Rust to compare .You may bring reusable codes at the end of the post.
[Table of Contents]
- What to do?
- Turn it into JavaScript Code with FP
- Vice Versa(char to numbers)
- Don’t repeat yourself with functions
- Conclusion
1. What to do?
I thought that I should get some url to reach the next proccess. So I assumed that I have to turn those numbers in characters.
First, I remembered that each numbers and characters have its own counterparts. So I need to tweak every numbers to corresponding characters.
Then, I have to join them into entire url not to type them manually.
Not so difficult when you already knew how to use JavaScript methods for array and string.
So let’s wrtie some code for that.
2. Turn it into JavaScript Code with FP
I thought that JavaScript FP methods would be greate candidate for this case. So I will use them for this example.
You may code with your browser console by ctrl + shift + i
or your code editor such as Visual Studio Code etc.

The above code snippet shows that those numbers were actually a link to some url.
We just made computer to tweak each number in array with map
keyword and String.fromCharCode(x)
after fat arrow to turn each number to a corresponding character.
Now we figured out where to go. It was https://www.medium.com.
But we wouldn’t want to type each character to do so.
We have to make them into string to easily copy and paste.
So the next phase is to join the array of each chracters to string.

We used join
JavaScript array method here to solve this.
and now we got url “https://medium.com” and easily go to the next phase.
3. Vice Versa(Characters to numbers)
Now You got what you wanted, but It is good to know how to do things oppositely at the same time.
Who knows that you would be a recruiter who needs to hire some developers?
Because you already know what matters here, I will include entire code snippets below.
You just need to know that we need codes what do exactly opposite things from the previous example.
They are split and charCodeAt
.
(You may search for yourself about split and charCodeAt.)
You may write code snippet like the example below.

4. Don’t repeat yourself with functions
Now You know everything I wanted to write for this article.
But It would be unefficient if you have to type those long words each time.
So you may save those process using fucntions.

When you make functiosn to do, You don’t have to repeat what you already know. You just pass the string “https://medium.com” as arguments and that is all.
I want to point out that it is alway better to simple type annotation as I did with (str = "")
to help others and computer to understand what happens.
You may make function for decode yourself.
It would be helpful for you to read articles for functions and arrow functions in JavaScript.
5. Conclusion
You did well. I hope this article helped you to learn how to turn numbers into characters and vice versa. You may let comments if there are parts not explained well or not correct.
Thanks for reading and please share this post.
p.s
- Did you like my coding style? Work with me. Please, send messages via LinkedIn.
- I am also developing React FrontEnd and Rust BackEnd Website www.steadylearner.com It lacks content but I would write it again together with real posts written here.
Further Reading
I am not a native English speaker. When some words don’t make sense, grammar problems etc. Please leave a comment below. I will read this post again and edit them right away.