
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Convert number to alphabet letter JavaScript
We are required to write a function that takes in a number between 1 and 26 (both inclusive) and returns the corresponding English alphabet for it. (capital case) If the number is out of this range return -1.
For example −
toAlpha(3) = C toAlpha(18) = R
And so on.
The ASCII Codes
ASCII codes are the standard numerical representation of all the characters and numbers present on our keyboard and many for.
The capital English alphabets are also mapped in the ascii char codes, they start from 65 and goes all the way up to 90, with 65 being the value for ‘A’, 66 for ‘B’ and so. We can use these codes to map our alphabets
The full code for doing this will be −
Example
const toAlpha = (num) => { if(num < 1 || num > 26 || typeof num !== 'number'){ return -1; } const leveller = 64; //since actually A is represented by 65 and we want to represent it with one return String.fromCharCode(num + leveller); }; console.log(toAlpha(18));
Output
The output in the console will be −
R
- Related Articles
- Swapping letter with succeeding alphabet in JavaScript
- Replace a letter with its alphabet position JavaScript
- How to Convert Letter Grade to Number in Excel?
- Possible combinations and convert into alphabet algorithm in JavaScript
- How to Convert Letter to Number or Vice Versa in Excel?
- How to shift each letter in the given string N places down in the alphabet in JavaScript?
- Change every letter to next letter - JavaScript
- Program to make vowels in string uppercase and change letters to next letter in alphabet (i.e. z->a) in JavaScript
- Convert number to characters in JavaScript
- Find letter's position in Alphabet using Bit operation in C++
- Check if input is a number or letter in JavaScript?
- How to convert Boolean to Number in JavaScript?
- How to convert Number to Boolean in JavaScript?
- How to convert Number to String in JavaScript?
- How to convert String to Number in JavaScript?

Advertisements