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

225
Visualizações
Why Math.max(NaN, <any_number>) returns NaN?

Why comparing NaN to any number in Math.max() returns NaN??

For example Math.max(NaN,5,3,7) returns NaN.

Also Math.max(Infinity, NaN, 5,3) return NaN too.

NaN is a falsy value.

But Math.max(false,4,5) does returns the largest i.e. 5.

I was getting some numbers from an API every second and compared the current fetched number with the previous one. But in some fetch it got NaN and after that the result was constantly NaN.

So what's the reason for this?

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

0

That is because the Math.max expect all the parameter to be a valid number.

As the documentation metioned:

If at least one of arguments cannot be converted to a number, the result is NaN.

The NaN is not considered a valid number, so the Math.max will output NaN

about 4 years ago · Juan Pablo Isaza Relatório

0

Math.max tries to convert passed values to numbers. If it cannot do that, it returns NaN.

+true is 1

+false is 0

Math.max(true, 0) returns 1

Math.max(Infinity, 1) returns Infinity

Math.max(NaN,5,3,7) returns NaN

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