EN
JavaScript - convert character to ASCII code
0 points
In this article, we would like to show you how to convert characters to ASCII code in JavaScript.
Quick solution:
xxxxxxxxxx
1
// in case we want to convert a single character
2
const charInASCII = 'your char'.charCodeAt(0);
Note: default argument of the
charCodeAt()
method is 0, socharCodeAt(0)
can be replaced withcharCodeAt()
.
In the example below, we convert characters to ASCII code at subsequent positions in the string.
xxxxxxxxxx
1
const exampleString = 'abc'
2
console.log(exampleString.charCodeAt(0)); // 97
3
console.log(exampleString.charCodeAt(1)); // 98
4
console.log(exampleString.charCodeAt(2)); // 99