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

153
Visualizações
Mark a number invalid (besides -1): Infinity or NaN?

I need to mark a number foo invalid and can't use -1 because foo can be negative. I still don't want to bother the compiler by changing the type. Both Infinity and NaN satisfy typeof === 'number', so I could use either? Are there any pitfalls with either?

Example: NaN != NaN (but there is Number.isNaN), but Infinity === Infinity.

I need to check whether the new number equals the old foo, so I would use NaN because it's shorter while semantically in my code both make sense.

Edit: Usage is to determine whether the cached value is still valid. (Note the original is written in Coffeescript.)

someCachedGetter: (() => {
  let foo = NaN; let cache = null;
  return () => {
    let t = getFoo();
    if (foo === t) { return cache; }
    else { foo = t; return cache = expensiveOperation(); }
}})(),
about 4 years ago · Juan Pablo Isaza
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