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.
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
As title
As I know
(if this part is true) && (this part will execute)
if(condition){
(this part will execute)
}
0 is false, so why not echo false but 0?
Because operator && return first falsey element otherwise they return last element
1 && 0 && false // 0
1 && 2 && 3 // 3
From MDN:
expr1 && expr2 -- Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.
expr1 || expr2 -- Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true.
!expr -- Returns false if its single operand can be converted to true; otherwise, returns true.
Some expressions that can be converted to false are:
null
NaN
0
empty string("" or '' or ``)
undefined
Short-circuit evaluation
As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:
false && (anything) is short-circuit evaluated to false.
true || (anything) is short-circuit evaluated to true.
The JavaScript documentation of logical operators explains:
Logical operators are typically used with Boolean (logical) values. When they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.
In javascript all except for null, undefined, false, 0, and NaN are Truthy.
In your case, why not echo false but 0?.
Javascript's ToBoolean function evaluates it to the first falsey value. i.e,
0 && true
=> 0
true && undefined
=> undefined
null && undefined
=> null
And if you need either strictly true or false, then go for not-not i.e, !!.
!!0 && true
=> false
!!true && undefined
=> false
!!null && undefined
=> false
(something falsy) && (anything) will always return false.
Also 0 is falsy and '0' (a non empty string) is truthy
a && b is equivalent to
if (a) {
return b;
} else {
return a;
}
So in your case a equals to 0, which is falsy, therefore you get a.
Unlike other languages, JavaScript does not return true or false on && and ||, it returns the first truthy operand for a || operator, or the last one, and the first falsy operand for the && operator, or the last one.
You can find more info here.
For && operator comparing with false is always FALSE.
0 && false => 0
false && 0 => false
for better understanding :-
In case of && operator (always start from left-right), when you get the value 0 (false) it will print 0(false); if start with false it will directly print false. it won't check the second operand when get false.
but in case
true && 1 => 1
1 && true => true
as it has to check till end and ultimately give the end operand as result if won't get false.
For || operator comparing with true is always TRUE.
1 || true => 1
true || 1 => true
for better understanding :-
In case of || operator (always start from left-right), when you get the value 1 (true) it will print 1(true).
Starting with true it will directly print true. it won't check the second operand when get true.
but in case
false || 1 => 1
0 || true => true
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
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.