Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
Make an array with every character on keyboard

I want to make an array containing all the letters and numbers on the keyboard (+ !, @, #, $, %, &, etc.) via code, without declaring it. Is there any way to do this in js?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

This will give characters that are displayable on the screen.

let characters = [];

for (let i=32; i<127; i++)
  characters.push( String.fromCharCode(i) );
about 4 years ago · Juan Pablo Isaza Relatório

0

I like 1 liners so

const characters = [...Array(95).keys()].map(i => String.fromCharCode(i+32))

Breakdown for those interested to know whats happening:

  • Array(95).keys() creates an iterator object with 95 items in it.
  • Because its wrapped in brackets []:
    • The spread syntax before it (...) iterates those objects into an array.
  • This results in a numbered array from 0 to 94. ie. [0,1,2,3,...,94]
  • Next you map the data from the array into the String.fromCharCode method.
    • Since we want only printable characters (starting from ascii code 32) we add 32 to each number in the source array before piping it into the fromCharCode method.

I've gotten really used to doing this for almost everything because its much faster than writing out a for loop for things. Granted it's slower than a for loop for performance (I think). Usually, I'm not writing code for performance.

about 4 years ago · Juan Pablo Isaza Relatório

0

There are 95 characters so you can make an array of 95 elements and map the items to the String char code that matches the index + 32.

const characters = Array.from(Array(95), (val,i) => String.fromCharCode(i+32))
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda