• 3 min

How To Generate an Alphabet JavaScript Array

How To Generate an Alphabet JavaScript Array
Table of Content

Imagine building a word game with JavaScript, and you need to pick a random character from the alphabet, but you don’t want to type all the letters by hand. (Please tell me you didn’t consider that 🙈). We will generate it with JavaScript .map, string and the Math.round methods.

No, we want to generate an Array with all the characters of the alphabet (I’m referring to the latin alphabet). We want to do that most straightforwardly and shortly as possible.

Let’s go.


Generate an Array with 26 items

First, we need an Array with 26 items because the Latin alphabet has 26 letters. We can do that simply with this code.

js
;[...Array(26)]// returns [...undefined, undefined]

Get a letter based on a number

Now that we have an Array with 26 items in it, we need to have a way to get letters based on this Array. So let’s create an Array with 26 numbers.

js
;[...Array(26)].map((_, i) => i)// returns [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]

Based on those numbers, we are going to get a letter. To achieve this, we need the String.fromCharCode() (check the MDN documentation for what you can do with the fromCharCode method).

js
;[...Array(26)].map((_, i) => String.fromCharCode(i))// returns ['\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\b', '\t', '\n', '\v', '\f', '\r', '\x0E', '\x0F', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19']

Get the correct letter from the alphabet

But those characters are not the ones we want. So we have to add something more. W3Schools offers a list of all the available letters. We can see that the “a” is on number 97. Now we can start counting.

js
;[...Array(26)].map((_, i) => String.fromCharCode(i + 97))// returns ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

Now we have our alphabet!


Choose between capital and lowercase characters

If you want capital letters instead of lowercase letters, you should start at position 65. Let’s make a function that supports both.

js
function generateAlphabet(capital = false) {    return [...Array(26)].map((_, i) => String.fromCharCode(i + (capital ? 65 : 97)))}

Inside the fromCharCodemethod, a ternary operator checks if capital is false or not. Notice that the default value of the parameter capital is false. When the value is true, it will return 65, when it’s false, it will return 97. So we can easily call the function like this generateAlphabet() without getting errors.

Try it on RunKit

If you want to have a random letter from the alphabet, you can achieve that by this.

js
function getRandomLetter() {    const alphabet = generateAlphabet()    return alphabet[Math.round(Math.random() * alphabet.length)]}

Try it on RunKit

I hope you learned something new or are inspired to create something new after reading this story!


Thanks!

After reading this post, I hope you learned something new or are inspired to create something new! 🤗

If I left you with questions or something to say as a response, scroll down and type me a message. Please send me a DM on Twitter @DevByRayRay when you want to keep it private. My DM’s are always open 😁

RayRay

I’m Ray, a Frontend Developer since 2009 living in the Netherlands. I write about Frontend Development, JavaScript, TypeScript, Angular, CSS, VueJS and a lot more related topics.

Want to reach me for comments or questions on my blog? Send a DM on Twitter @DevByRayRay