Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda