JavaScript

How to Convert Character Code to ASCII Code in JavaScript?

Converting character codes to ASCII codes in JavaScript is very useful as it is the standard encoding format for electronic communication among computers. Moreover, the ASCII code is universally accepted and convenient in the programming area as well. In addition to that, it also does wonders in encoding some sort of data for ensuring data privacy.

How to Convert Character Code to ASCII Code in JavaScript?

The following methods can be applied to convert character code into ASCII code representation in JavaScript:

  • charCodeAt()” method.
  • codePointAt()” method.

In the section below, the mentioned approaches will be illustrated one by one!

Method 1: Convert Character Code into ASCII Code Representation in JavaScript Using the charCodeAt() Method

The “charCodeAt()” method gives the character’s Unicode with respect to the specified index. This method can be utilized to convert the provided character into ASCII code by simply pointing to its index.

Syntax

string.charCodeAt(index)

In the given syntax:

  • index” refers to the index of character.

Example 1: Converting Character Code into ASCII Code Representation Using JavaScript
In this example, go through the following code snippet:

let char = "x"
console.log("The converted ASCII code is:",char.charCodeAt(0))
  • First, initialize the variable named “char” with the specified character value.
  • Now, apply the “charCodeAt()” method by referring to its index i.e “0”.
  • This will result in converting the character “x” to its corresponding ASCII code which is “120” in this case.

Output

Example 2: Converting Character Code into ASCII Code Representation Using JavaScript
In this example, convert the character to ASCII code by extracting it from a string value.

The following demonstration explains the stated concept.

let string = "Linuxhint"
console.log("The converted ASCII code is:", string.charCodeAt(2))
  • Firstly, initialize the string value as discussed in the previous example.
  • After that, apply the “charCodeAt()” method by passing the desired character’s index as its parameter.
  • This will result in converting the character “n” to ASCII code as specified by the index.

Output

Method 2: Convert Character Code to ASCII Code in JavaScript Using the codePointAt() Method

The “codePointAt()” method returns the character’s “Unicode” value at the string’s particular index. This method can also similarly be applied to the previous method by referring to the character’s index.

Syntax

codePointAt(index)

In the given syntax:

  • index” refers to the index of the character in a string.

Example 1: Converting Character Code into ASCII Code Representation in JavaScript
Go through the following code snippet:

let char = "a"
console.log("The converted ASCII code is:", char.codePointAt(0))

Follow the below-stated steps:

  • In the first step, allocate a character to the variable named “char”.
  • Now, apply the “codePointAt()” method by referring to the character’s index which will similarly convert the corresponding character to ASCII code.

Output

Example 2: Converting Character Code From String into ASCII Code Representation Using JavaScript
This specific example will result in converting the character to ASCII code by extracting it from a string value.

The below-given code-snippet explains the stated concept:

let string = "David"
console.log("The converted ASCII code is:", string.codePointAt(4))

Follow the below-stated steps:

  • Store the following string value in a variable named “string
  • Finally, apply the “codePointAt()” method by passing the index of the character “d” in this case.
  • This will return the ASCII code representation of the indexed character.

Output

We have concluded the approaches to convert character code into ASCII code representation

Conclusion

The “charCodeAt()” method or the “codePointAt()” method can be applied to convert character code or the extracted character code from string to ASCII code in JavaScript. Both methods return the same outcome (ASCII representation) by indexing to the character in a string. This tutorial demonstrated the approaches for converting character code into ASCII code representation using JavaScript.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.