How to convert character to/from keycode in javascript examples

This tutorial shows 2 ways to convert keycode to/from the character in javascript.

One way to charCodeAt() function convert character to keycode character, Another fromCharCode() function convert unicode keycode to character.

Key codes for letters are unique numbers for any characters including Unicode characters.

Keycodes and characters are

97-122 for a-z characters 65-90 for A-Z characters. 48-57 for 0-9

How to Convert character to keycode in javascript

charCodeAt() method returns returns keycode for an character.

It returns numbers

97-122 for a-z characters 65-90 for A-Z characters. 48-57 for 0-9

console.log('a'.charCodeAt(0)) // 97
console.log('z'.charCodeAt(0))// 122
console.log('A'.charCodeAt(0))// 65
console.log('Z'.charCodeAt(0)) //90

How to get characters for a given keycode in javascript?

String.fromCharCode() takes one or many keycodes and return characters



Arguments are one or more keycode characters. Returns Character

Here is an example

console.log(String.fromCharCode(97)) //a
console.log(String.fromCharCode(122)) //z
console.log(String.fromCharCode(65,90)) //AZ


In a summary, Learned ways to convert characters to/from keycode in javascript with examples.

Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.