How to Convert String to Char Code in Javascript

To convert a string to char code in JavaScript, use the charCodeAt() method. The charCodeAt() is a built-in JavaScript method that takes an index of characters from the string we want to convert into the Unicode.

Before converting our string to charCode, let’s understand what char code is?

What is char code in JavaScript?

In JavaScript, every character represents one number, calling them ASCII numbers. Javascript has 127 characters. We get this number by using the javascript charCode property. It returns a  Unicode character. The “a” character represents 97 numbers in JavaScript.

JavaScript String charCodeAt()

The charCodeAt() is a built-in JavaScript method that returns the Unicode of the character at a specified index (position) in a string. The index of the first character is 0, and the second is 1. The index of the last character is string length – 1.

Syntax

string.charCodeAt(index);

Parameters

Index – It is an index number of our string. By default, it is 0.

Return type

Number:  It will return a Unicode of our character. If our index is not valid, then it will return NaN.

Example

const string = "Wonderful People";

console.log(string.charCodeAt(0));
console.log(string.charCodeAt(5));
console.log(string.charCodeAt(8));
console.log(string.charCodeAt(32));

Output

87
114
108
NaN

In the above example, we pass an index of the string, and the charCodeAt method returns the Unicode of that character. If we don’t give anything inside that, it will return the Unicode of the first character of our string.

Convert string to charCode using map() function

The map() is a built-in array function that creates a new array populated with the results of calling a provided function on every element in the calling array.

const string_to_char_code = ([...string]) => {
   const array = string.map((char) => {
   return char.charCodeAt(0);
 });
 // array of unicode
 return array;
}
console.log(string_to_char_code("Wonderful"));

Output

[87, 111, 110, 100, 101, 114, 102, 117, 108]

In the above example, we convert our whole string to an array of Unicode using a map, and it is a simple way to do that.

Let’s do something interesting with that charCodeAt() method. In the below example, we do the sum of all Unicode.

const string_to_char_code = ([...string]) => {
   const array = string.map(char => char.charCodeAt(0));
   const sum = array.reduce((cur, prev) => cur + prev);
   return sum;
}
console.log(string_to_char_code("Wonderful People"));

Output

1595

Converting Unicode to char code in JavaScript

To convert a Unicode to char code in JavaScript, use the fromCharCode() method. The String.fromCharCode() is a built-in JavaScript method that returns a string created from the specified sequence of UTF-16 code units.

Syntax

String.fromCharCode(P1, P2, …);

Arguments

P1, P2, P3: These methods need one or more Unicode values separated by commas.

Return value

String: It returns a string of Unicode, which we pass.

Example

const character = String.fromCharCode(86);
console.log(character);

const str = String.fromCharCode(87, 111, 110, 100, 101, 114, 102, 117, 108);
console.log(str)

Output

V
Wonderful

In the above example, we convert our Unicode to the character string using the fromCharCode() function.

That’s it for this tutorial.

See also

How to Convert URL to String in JavaScript

How to Convert JavaScript String to Array

How to Convert Object to String in JavaScript

Leave a Comment