Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!