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

128
Visualizações
Very strange behaviour of parseInt and map

Take a look at this experiment conducted on jsconsole.com

> parseInt("00")
0
> ["00"].map(parseInt)
Array (1)[ 0 ]
> ["123", "00"].map(parseInt)
Array (2)[ 123,NaN ]
> ["00", "123"].map(parseInt)
Array (2)[ 0,NaN ]

I've stumbled upon this by trying to do some date-time parsing. The problem seemed to be the fact that I didn't specify which integer base I want. Using parseInt(x, 10) solves my problem but could someone explain to me how ["00"].map(parseInt) and ["123", "00"].map(parseInt) could possibly differ for the value of "00"? I might be missing something extremely obvious here, but if I'm not this would mean that there's something wrong with Array.prototype.map or parseInt or both.

Thanks in advance.

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

0

It has to do with the extra parameters map adds to the method call. Try the following snippet to get the correct results. This prevents parseInt from receiving them.

console.log(["123", "00"].map(it=>parseInt(it)))

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