If [] is falsy, how come ![] is also falsy [duplicate] - javascript

This question already has answers here:
Empty arrays seem to equal true and false at the same time
(10 answers)
Closed 3 years ago.
We know that in JS [] == false returns true. But today I was watching a video presentation by Kyle Simpson and I saw this:
[] == ![] // true
How come ![] is also falsy. As per my logic, it should be true.

== allows coercion. So does ! operator. So [] == ![] becomes "" == false which is true. Further ![] is true because [] is an object which is a true value. And negation of that yields false.

In Javascript, not operator (!) check for not condition like this !(true), here first it checks the inner part of (), If it returns true then the actual output becomes false.
(true) => !(true) =>false
(false)=>!(false)=>true`
Get back to original question
[] return true where as ![] return false when you compare strictly with === its check for type and value both.
console.log([]); // true
console.log(![]); // false
console.log([] == ![]) // true
console.log([] ===![]) // false
Note if you want boolean value all the time then you can use !!.
console.log(!![]); // true
console.log(!!(1 === 1)); // true

Related

Why isNaN(true) ( or (isNaN(false) ) is returning false? [duplicate]

This question already has answers here:
Why is isNaN(null) == false in JS?
(9 answers)
Closed 3 years ago.
Just a simple question...
Why this:
isNaN(true)
or this:
isNaN(false)
return false ?
I can understand that false is evaluate to 0 and true to 1 or something...
But is it not the purpose of isNaN(...) to return false only if a variable or a value is "exactly" a number ?
As you think, they return false, because isNaN first coerces its argument into a Number, and then it returns whether the argument is NaN.
Number(true) is 1 and Number(false) is 0, so neither of them is NaN.
To perform a stricter check, and avoid implicit coercion, call Number.isNaN instead:
Both Number.isNaN(true) and Number.isNaN(false) returns true.

Checking if a variable is either true or false [duplicate]

This question already has answers here:
How to check if type is Boolean
(18 answers)
Closed 3 years ago.
In JavaScript I have a variable initialized with undefined. Now I'd like to check if it is either true or false.
Is there a statement more elegant than the following?
if (isValid === true || isValid === false) {
// do something
}
How about checking if it is not undefined?
If it is neither true, nor false it has to be undefined.
So if it is not undefined, it is set and therefore either true or false.
if (isValid !== undefined) {
// do something
}

When and why do you use !! in javascript? [duplicate]

This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 6 years ago.
I came across a javascript program which was using !! operator for comparision. I know ! stands for NOT EQUAL. So logically !! means NOT OF NOT EQUAL which is EQUAL.
if (!!var_1){
//...
}
My question is why do people sometimes use !! and not == operator ?
I've read similar questions, but couldn't figure out when exactly do we use this.
!! is not an operator, it's just the ! operator twice.
!oObject //Inverted boolean
!!oObject //Non inverted boolean so true boolean representation
Some output examples:
alert(true); // Gives true
alert(!true); // Gives false
alert(!!true); // Gives true
alert(!!!true); // Gives false
alert(false); // Gives false
alert(!false); // Gives true
alert(!!false); // Gives false
alert(!!!false); // Gives true
You see, one "!" just changes is from false to true or the other way around. With two or more of the "!", the process is simply repeating and changing its value again.

double exclamation on a return value in javascript [duplicate]

This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 9 years ago.
I'm looking over a book for html 5 and it includes this tid bit of js. What does double exclamation mean?
return ! ! document.createElement('video').canPlayType;
The ! operator negates, and the secondary ! negates the result of the inital negation. This basically typecasts whatever is on the right hand side into a boolean ( true or false ).
!false // true
!!false // false
So if the method is defined then the function which is truthy, will be typecasted into true.
document.createElement('video').canPlayType
So the above returns a function. We don't want a function, we want an explicit boolean, so we negative it twice and since function is not falsy ( 0, null, false, empty string, NaN ) then it returns true for browsers which support this method.
!!document.createElement('video').canPlayType // true
If the method is not supported, the browser will return undefined. !undefined is true, and !true is false, so !!document.createElement('video').LOL will return false
The double exclamation is a JavaScript trick to return true/false regardless of input. In your example, if a browser does not support the video tag, it will return undefined. !undefined is true. So !!undefined is false. If the browser does support video, it will return true. !!true is also true. This saves you from having to handle the case of undefined.

When should you use === vs ==, !== vs !=, etc.. in javascript? [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
What are the differences between === vs == and !== vs !=?
When should you use each one?
=== is the Identity operator, and is used to test that value and type are equal.
so..
"3" == 3 // true
"3" === 3 // false
1 == true // true
1 === true // false
"1" == true // true
"1" === true // false
so when you care that value and type are equal, or not equal use Identity operators === or !==
The "normal" == operators in javascript perform type coercion, and try their best to do things like treat a string as number or an object as a string where required. The longer === operators will not do type coercion, but rather a strict comparison within the type.
=== and !== are the same as == and !=, but additionally do checks for the variable types.

Categories

Resources