Is it a good way for getting alphabet in my project?

Viewed 41

I did this for getting the alphabet. Is is a fast and good way to do ?

function alphabet ( ) {
    const alphabet = []
    for(let i=97; i<123; i++) {
        alphabet.push(String.fromCharCode(i))
    }
    return alphabet
}

console.log(alphabet())

How would you write it if you had to ?

1 Answers

Instead of using this way, you can also split a string and get it. Two ways here:

  • String.split()
  • Array.from()

const abc = "abcdefghijklmnopqrstuvwxyz";
console.log(abc.split(""));
console.log(Array.from(abc));

Both works.

Related