27

Why JS developers go bald sooner! ?#%*

Comments
  • 3
    NaN is a String that gets returned when you console.log it. Try console.log($scope.edad.toString === 'NaN');. undefined works the same way.
  • 3
    Try {} == {}. Will always be false because they refer to different references. One NaN will never be equal to a different NaN. Instead use isNaN(variable)
  • 1
    @tytho Reference comparison is why I suggested comparing strings. wouldn't objectifying w/ {} force reference comparison? I honestly am not sure, I'm pretty junior at JS but I just starting working in MEAN so I've gotten a bit of practice lately.
  • 2
    @lurch I'm sure you could stringify an object for object comparison, but that could be a heavy operation depending on how big your objects are. It also would probably give you a lot of inconsistent behavior since object keys don't have a guaranteed order.

    For example, you have an object { a: 1, b: 2 }, and another one { b: 2, a: 1 }. By our definition, they're "equal", but they would stringify as different strings. The NaN example would work for this if you're strictly checking to see if a number is NaN, but the correct way to do it would be to use the more predictable isNaN since you may run into an object that can't be coerced into a string.
  • 0
    I'm pretty sure what you want is the isNaN function https://developer.mozilla.org/en-US... just one of those things you learn along the way.
  • 0
    By the way kind of off topic but are you philipino? And is that Tagalog?😃
  • 1
    @johnfoobar that is probably spanish (for "age")
  • 1
    @JoseHdez2 ah okay that makes sense. Tagalog has loan words from Spanish, and I guess edad is one of them. It age in Tagalog as well.
  • 0
    NaN is a number. If you wanna test it you should use Number.isNaN(NaN) //true

    (console.log(typeof(NaN))//return 'number'
Add Comment