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

169
Visualizações
Why do includes() and indexOf() work differently with searching NaN?

//indexOf()
console.log([1, 2, NaN].indexOf(NaN)); //-1

//includes()
console.log([1, 2, NaN].includes(NaN)); //true

I don't get why includes can find NaN in array, while indexOf can't.

I've read that it's happening because indexOf uses strict-equality-operator internally (NaN === NaN). But as for me, there's no deffirence between strict-equality-operator and abstract equality operator when we're talking about NaN. NaN can't be equal to itself anyway.

//strict equality operator
console.log(NaN === NaN); //false

//abstract equality operator
console.log(NaN == NaN); //still false

So why is there some difference between these 2 methods?

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

0

Basically, Array.prototype.indexOf uses a different equality algorithm than Array.prototype.includes.

Array.prototype.indexOf ends up using Number::equal. Which does:

  1. If x is NaN, return false.
  2. If y is NaN, return false.

While Array.prototype.includes ends up using Number::sameValueZero

Which does:

  1. If x is NaN and y is NaN, return true.

As to why does the spec allow a comparison of two different NaNs to be true, well that's up to the TC. I can only guess they made it this way as it's a convenient way to check if there's a NaN in the array.

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