I can't seem to understand why when I put in the console
isNaN == true results to false while !isNaN == false returns to true.
When
NaN == true
false
NaN == false
false
Sorry I'm kinda new and somewhat confused.
isNaN is a function which determines if the value is NaN. So isNaN will check if the input is a number or not .
For example isNaN(5) == true will return false , because 5 is a number, Simillarly for empty string isNaN('') == true will also return false it is not a number.
But for isNaN('Hello') == true will return true, since 'Hello' is not a number
Now when using negation(!) it will work oppositely
The isNaN() function determines whether a value is NaN or not.
its return true if the given value is NaN; otherwise, false.
see below link for more info
Related
This question already has answers here:
What does "!" operator mean in javascript when it is used with a non-boolean variable?
(5 answers)
Closed 2 years ago.
I looked a lot, but I couldn't find an answer for this especific case.
Why does this expression return true?
let variable = 0
!variable // true
I understand that the ! mark checks if a value is null or undefined, but in this case variable is defined. This is tricking me.
Isn't 0 really considered a valid value?
! is known as the logical NOT operator. It reverses the boolean result of the operand (or condition)
0 is also considered as the boolean false, so when you use !variable you are using the logical operator and saying it to change the value of the variable to its opposite, that in boolean is true
0 == false == !1 == !true
1 == true == !0 == !false
in Javascript are considered false:
false, null, undefined, "", 0, NaN
are considered true:
true, 1, -0, "false". <- the last one is a not empty string, so its true
if( false || null || undefined || "" || 0 || NaN) //never enter
if( true && 1 && -1 && "false") //enter
https://developer.mozilla.org/en-US/docs/Glossary/Falsy
To quote MDN Web docs, the Logical NOT !:
Returns false if its single operand can be converted to true; otherwise, returns true
So in your case it returns true because 0 can be converted to false
You can check out this link:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
console.log(false === 0) // false
console.log(false === !1) // true, why does it equate to true using !?
and vice versa for
console.log(true === 1 ) // false
console.log(true === !0) // true
I understand the difference between equality and identity but couldn't understand this behaviour of JS . Please explain ?
The === requires that both values are the same type, no implicit coercion is performed.
When you compare a boolean to a number with triple ===, you will always get false, since they are not the same type.
But using ! in front of a number (or anything else) will convert it to Boolean first, (!x is same as !Boolean(x)) so the strict comparison can succeed. (actually, using ! on anything will coerce it to a Boolean in JS)
Rules of conversion number-to-boolean conversion :
For the number-to-boolean conversion, 0 and NaN are coerced to false, and any non-zero number is coerced to true. (FYI, null, undefined and the empty string '' are the only other falsy values in JS)
Now, you will have two boolean to compare, and === will return true or false accordingly.
So, to summarize :
in !1, 1 is non-zero, so it gets coerced to true, and !true gives false
so false === !1 is equivalent to false === false, which is a true statement.
You can work out the details for the other comparison.
As an additional resource, if you have time and are interested in learning more, I recommend the very good free ebook "You don't know JS".
Because !1 is treated as a boolean value in JS whereas 0 or 1 are interpreted as number.
You can use JS typeof !1 and typeof 0 to see the difference.
Since, false is also a boolean type, matching it with 0 (number) will result false.
Hope that clears it up.
When you do false === 0, it's comparing the boolean false to the integer 0. In contrast, when you do false === !1, javascript converts the integer 1 to a boolean and the unary ! acts as a "not" operation on what is effectively true. Since 1 is truthy in javascript, !1 converts to false. And vice-versa.
console.log(0) // 0
console.log(!0) // true
console.log(1) // 1
console.log(!1) // false
!1 is a Boolean (because of the logical NOT operator (!)).
true and false are also Boolean values.
0 is a Number.
Values of different types are never identical (===).
JavaScript will first convert your inverted numer to a boolean:
console.log(false === 0)
-> false
console.log(false === !1)
-> false === false
-> true
and vice versa for
console.log(true === 1 )
-> false
console.log(true === !0)
-> true === true
-> true
Do you understand the difference between an integer and a boolean?
console.log(false === 0) // false
console.log(false === !1) // true
console.log(true === 1 ) // false
console.log(true === !0) // true
console.log(!1) // false
console.log(!0) // true
Identity comparison checks that an object is exactly that, and 1 is not true, neither is 0 false.
Because ! operator convert any value to Boolean type and you are checking against another Boolean that's why false === !1 is giving you true.
Why NaN === false => false, isn't NaN falsy?
Why NaN === NaN => false, but !!NaN === !!NaN => true
I've been racking my brain trying to figure this out.
Falsy and being strictly equal to false are very different things, that's why one has a y instead of an e. ;)
NaN is spec'd to never be equal to anything. The second part of your question is comparing false === false, which is funnily enough, true :)
If you really want to know if something is NaN, you can use Object.is(). Running Object.is(NaN, NaN) returns true.
1.Why NaN === false => false, isn't NaN falsy?
The term "falsy" isn't defined in ECMA-262, it's jargon for where type conversion coerces a value to false. e.g.
var x = NaN;
if (!x) {
console.log('x is "falsy"');
}
The strict equality operator uses the Strict Equality Comparison Algorithm which checks that the arguments are of the same Type, and NaN is Type number, while false is Type boolean, so they evaluated as not equal based on Type, there is no comparison of value.
2.Why NaN === NaN => false, but !!NaN === !!NaN => true
Because the strict equality comparison algorithm states that NaN !== NaN, hence the isNaN method.
Using ! coerces the argument to boolean using the abstract ToBoolean method, where !NaN
converts to true and !!NaN converts to false, so:
!!NaN === !!NaN --> false === false --> true
Note that the abstract equality operator == will coerce the arguments to be of the same Type according to the rules for the Abstract Equality Comparison Algorithm. In this case, NaN is Type number, so false is converted to a number using toNumber which returns 0. And 0 is not equal to NaN so:
NaN == false --> NaN == 0 --> false
This condition:
NaN === false
Is always false because numbers are not booleans. To test if a value is falsy you can use a ternary expression:
NaN ? "truthy" : "falsy" // falsy
Why NaN === NaN => false
This is explained in MDN; pragmatically speaking, though, two values of which you only know they're not numbers can't logically be the same thing.
... but why is !!NaN === !!NaN => true
This is because casting NaN into a boolean will make it false and booleans can be compared as per normal.
=== compares both type and value.
Even though NaN is falsey, === wouldn't be the way to compare it. Something is "falsey" if it evaluates to false in a boolean expression. That isn't the same as being equal to (or equivalent to) false.
For example, null == false returns false, even though null is falsey. This is not completely intuitive, but that's just how JavaScript handles false/falsey values.
0 and the blank string ("") are special cases where value equality comparisons against false evaluate to true (i.e. 0 == false and "" == false). However, 0===false and ""===false still returns false.
NaN is special in that it doesn't have a real value, so comparing it to itself doesn't return true. Essentially, NaN is equal to nothing, not even NaN.
The only way to reliably compare something to NaN is using isNaN( value ).
To your second point, !value is a boolean expression. value first undergoes type coercion to a boolean (and remember, NaN is falsey) and then the boolean NOT ! makes it true. As it happens, it's double negated, so !!NaN is the same as boolean false. Of course false === false, so the expression evaluates to true.
Why NaN === false => false, isn't NaN falsy?
NaN as you are using, is a global property initialized with value of Not-A-Number. It's not boolean. It's NaN data type as defined by IEEE 754.
It's the "same thing" you compare null === false (or even null == false).
In this case, there is no difference using sctric equal or not: NaN == false, also will return false!
Why NaN === NaN => false, but !!NaN === !!NaN => true
2.1. NaN ==== NaN, is false by definition.
2.2. But in !!NaN === !!NaN, you aren't comparing NaNs anymore, when you do ![value], you "evaluate" it (or cast to a boolean).
I'm gonna now explain with null, because it's more used, so you can apply it to NaN:
Casting NaN, it's the same thing that casting null.
null == false? true : false // false! because it's not a bool false.<br>
!null? true: false // true! because !null -> true -> if(true)...
More Refs:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.1.1
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN
This comparison also returns false:
const x = NaN
const y = x
console.log(x === y) // false
NaN and false are both the subset of Falsy.
NaN == false -> they're both falsy -> thus 'TRUE'
NaN === false -> they're NOT the same type -> thus 'FALSE'
what does the term [0] == ![0] means? Though they return true.But i need to explain how it returns true as type of [0] is object and ![0] returns boolean? So how they are equal? Thanks
![0] is simply false, since all non-null objects cast to true.
When comparing [0] and false, they are converted to numbers - don't ask why, that's just the way it is. [0] is first converted to the string "0" (arrays cast to strings by concatenating the entries with , for a separator), which is then the number 0. false is cast to the number 0, and there you have it: [0] == ![0] is equivalent to 0 == 0, which is true.
To understand this, go through ![0] expression first. It evaluates to false - as [0] (as any Object in JS) is a truthy value. So the statement becomes...
[0] == false
Now it's easier: false is converted to 0 (for Boolean -> Number rule), and [0] is converted by Object-To-Primitive rule - first to '0' (String), then to 0 (Number). Obviously, 0 is equal to 0. )
P.S. And yes, it may seem quite weird, but both...
[0] == false
... and ...
![0] == false
... evaluate to true: the former is already explained, the latter is just false == false. Anyone still surprised by those == Lint warnings? )
You have split the expression into multiple parts:
typeof([0]) // "object"
[0] == true // false
![0] == true // false
![0] == false // true
The reason for this because in JavaScript only the value 1 is implicitly converted to true, so all other values are converted to false. The ![0] only negates a false expression thus it becomes (false == false) == true.
I got the Json "false" from server. I respond as bool but it's Json so it's in browser type is String instead of bool.
So if I run (!data) whenever I want to check "false" == false then they not worked.
So how can I parse bool from String in JavaScript then?
"true" == true and "false" == false. Then the code (!data) can check what it is [true and false]
If one of the operands is a boolean, convert the boolean operand to 1 if it is true and +0 if it is false.
When comparing a number to a string, try to convert the string to a numeric value.
from MDN Equality Operators page
Examples:
true == "true"; // 1 == NaN → false
true == "1"; // 1 == 1 → true
false == "false"; // 0 == NaN → false
false == ""; // 0 == 0 → true
false == "0"; // 0 == 0 → true
I would just explicitly check for the string "true".
let data = value === "true";
Otherwise you could use JSON.parse() to convert it to a native JavaScript value, but it's a lot of overhead if you know it's only the strings "true" or "false" you will receive.
var data = true;
data === "true" //false
String(data) === "true" //true
This works fine.
Try expression data == "true"
Tests:
data = "false" -- value will be false
date = "true" -- value will be true
Also, fix your JSON. JSON can handle booleans just fine.
If its just a json "false"/"true", you can use,
if(! eval(data)){
// Case when false
}
It would be more cleaner, if you restrict the code to accept only JSON data from server, and always jsonParse or eval it to JS object (something like jquery getJSON does. It accepts only JSON responses and parse it to object before passing to callback function).
That way you'll not only get boolean as boolean-from-server, but it will retain all other datatypes as well, and you can then go for routine expressions statements rather than special ones.
Happy Coding.
I think you need to look at how the JSON data is being generated. You can definitely have a normal JS boolean false in JSON.
{ "value1" : false, "value2" : true }
String.prototype.revalue= function(){
if(/^(true|false|null|undefined|NaN)$/i.test(this)) return eval(this);
if(parseFloat(this)+''== this) return parseFloat(this);
return this;
}
From: http://www.webdeveloper.com/forum/showthread.php?t=147389
Actually, you just need the first "if" statement from the function -- tests to find true or false in the code and the evals it, turning it into the boolean value
if(data+''=='true'){
alert('true');
}
Convert boolean to string by appending with blank string. and then compare with Stringobject.