What logic is behind NaN evaluation? [duplicate] - javascript

This question already has answers here:
Why is NaN === NaN false? [duplicate]
(3 answers)
Closed 8 years ago.
Can you explain the logical evaluation of NaN in javascript?
NaN==NaN (false)
!NaN==!NaN (true)
NaN!=NaN (true)
NaN==false (false)
NaN==true (false)
I am a little surprised here...

Because that's what the spec says.
Specifically, the IEE floating point spec says that NaN does not equal itself.
Therefore, in Javascript, there is no value that NaN can ever equal.
!NaN is true, because NaN is a falsy value.
This makes all of your examples obvious.

Related

Javascript: null==0 returns false, but null<1 returns true. Please explain [duplicate]

This question already has answers here:
Why is null in JavaScript bigger than -1, less than 1, but not equal (==) to 0? What is it exactly then?
(2 answers)
Closed 2 years ago.
very weird behaviour indeed.
Please explain the implicit conversions that happen in each case.
null will be converted to the number 0 (because < works with numbers), and 0 is less than 1.

How NaN==NaN returns false? [duplicate]

This question already has answers here:
What is the intuitive reason that NaN != NaN?
(1 answer)
Why is NaN === NaN false? [duplicate]
(3 answers)
Closed 4 years ago.
NaN==NaN
returns false in javascripts. i want to know some clear explanation.
typeof NaN is number
Number == Number is return true
Then why NaN==NaN is false. Thanks advance.

Difference output for "" and " " in javascript [duplicate]

This question already has answers here:
Understanding JavaScript Truthy and Falsy
(9 answers)
Is empty string treated as falsy in javascript?
(5 answers)
Closed 4 years ago.
I have tried 2 conditions in JavaScript and output:
if(""){console.log("Called")} //No Output
if("_"){console.log("Called")} //Output: Called
What could be the possible reason for this?
The empty string is considered as a 'falsy' value, and so it's equivalent to doing:
if(false){console.log("Called")}
The empty string is equal to False if you cast it to Boolean (Type Conversion).
console.log(Boolean("")) //output: false
console.log(Boolean("somestring")) //output: true

JavaScript less than or equals (bool <= int) [duplicate]

This question already has an answer here:
How do the JavaScript relational comparison operators coerce types?
(1 answer)
Closed 8 years ago.
I am wondering if anyone can explain the behavious of comparison operators when using booleans and integers.
Why do the following statements produce the results they do?
false < = 9 // true
false >= 9 // false
Thanks
The representation of false in memory is 0. That's why you obtain those results.

Why is NaN === NaN false? [duplicate]

This question already has answers here:
What is the rationale for all comparisons returning false for IEEE754 NaN values?
(12 answers)
Closed 6 years ago.
Why does NaN === NaN return false in Javascript?
> undefined === undefined
true
> NaN === NaN
false
> a = NaN
NaN
> a === a
false
On the documentation page I see this:
Testing against NaN
Equality operator (== and ===) cannot be used to test a value against NaN. Use isNaN instead.
Is there any reference that answers to the question? It would be welcome.
Strict answer: Because the JS spec says so:
If Type(x) is Number, then
If x is NaN, return false.
If y is NaN, return false.
Useful answer: The IEEE 754 spec for floating-point numbers (which is used by all languages for floating-point) says that NaNs are never equal.
This behaviour is specified by the IEEE-754 standard (which the JavaScript spec follows in this respect).
For an extended discussion, see What is the rationale for all comparisons returning false for IEEE754 NaN values?
Although either side of NaN===NaN contains the same value and their type is Number but they are not same. According to ECMA-262, either side of == or === contains NaN then it will result false value.
you may find a details rules in here-
http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3

Categories

Resources