js number to alphabet code example

Example 1: how to make an alphabet in javascript

// To get an array of letters from 'a' to 'z' in javascript.

let alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');

Example 2: javascript number in alphabet

// converts numbers to spreadsheet letter columns eg. 1 -> A
function numToSSColumn(num){
  let s = '', t;

  while (num > 0) {
    t = (num - 1) % 26;
    s = String.fromCharCode(65 + t) + s;
    num = (num - t)/26 | 0;
  }
  return s || undefined;
}

numToSSColumn(0); // undefined
numToSSColumn(1); // A
numToSSColumn(26); // Z
numToSSColumn(-1); // undefined
numToSSColumn(27); // AA
numToSSColumn(475254); // ZZZZ

Example 3: javascript alphabet to number

function convertLetterToNumber(str) {
  str = str.toUpperCase();
  let out = 0, len = str.length;
  for (pos = 0; pos < len; pos++) {
    out += (str.charCodeAt(pos) - 64) * Math.pow(26, len - pos - 1);
  }
  return out;
}

convertLetterToNumber("A"); // 1
convertLetterToNumber("b"); // 2
convertLetterToNumber("Ba"); // 53
convertLetterToNumber("dE"); // 109
convertLetterToNumber("ZZZZ"); // 475254

Example 4: js int to alphabet

var value = 10;

document.write((value + 9).toString(36).toUpperCase());