get char code javascript code example

Example 1: get ascii value of char javascript

word = userInput[0]; //abc
var sum =0;
for(i=0;i<word.length;i++)
{
    sum = sum + word.charCodeAt(i);
}
console.log(sum); //294

Example 2: javascript charcode

const sentence = 'The quick brown fox jumps over the lazy dog.';

const index = 4;

console.log(`The character code ${sentence.charCodeAt(index)} is equal to ${sentence.charAt(index)}`);
// expected output: "The character code 113 is equal to q"

Example 3: charcodeAt javascript

const sentence = 'The quick brown fox jumps over the lazy dog.';

const index = 4;
// return the chartcode of the character with index N°4
const charcode= sentence.charCodeAt(index);
console.log (charcode);  
// expected output: "The character code 113 is equal to q"

// remember that charCodeAt return the code on ♦ keypress event ♦