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

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.

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.

Is !!!foo the same as !foo? [duplicate]

This question already has answers here:
The use of the triple exclamation mark
(4 answers)
What is an exclamation point in JavaScript?
(2 answers)
Closed 3 years ago.
Knowing that !!foo gives you the Boolean value of foo, I have seen some programmers say that it's better to use !!!foo instead of !foo because you are changing it to its Boolean value first and then negating the value.
So my question is,
Is !!!foo always equals !foo? (!!!foo === !foo)
Yup. Just for clarity:
!!x === x is not generally true, but it is true if x is already a boolean: "not (not true)" is true, and "not (not false)" is false.
!foo is always a boolean; if foo is truthy, it's false, otherwise it's true.
So if you substitute !foo in for x you get that !!(!foo) === (!foo) is always true. Removing the parentheses doesn't change the meaning, so !!!foo === !foo is always true.
Which means there's no good reason to write !!!foo in actual code. Just use !foo instead.
I've never seen !!!foo used in actual code, but yes, they are always strictly equal.
The extra two exclamation marks just negate the boolean value twice, which always gives the same result.
If foo is set as boolean then !foo will be equal to !!!foo , See the example and screenshot given below
var foo = true;
console.log('foo => '+foo);
console.log('!foo => '+!foo);
console.log('!!foo => '+!!foo);
console.log('!!!foo => '+!!!foo);
console.log('!!!!foo => '+!!!!foo);
if(!!!foo === !foo){
console.log("Equals");
}else{
console.log("Not Equals");
}
While you are correct, !!!foo would equal to !foo, I've never seen JS script using !!!foo before, and I can't think of a good reason to do that. You're gonna create code smell if you write this in your own code.
In other words, don't do that.

What is the purpose of using !! in JavaScript? [duplicate]

This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 9 years ago.
I want to know the purpose of using !! in JS.
For ex:
this.enabled = function (value) {
if (arguments.length) {
enabled = !!value;
}
}
It's not related to angular
It's just a way to transform value to bool. ( according to truthy/falsy values)
There is a lot of articles about it.
examples :
!!"a" //true
!!"0" //true
!!0 //false
To be clear, this question has nothing to do with AngularJS--it is a JS syntax question.
The purpose of !! in JavaScript (and other langs) is to force a value to boolean.
Using a single ! forces it to boolean, but opposite of whether the value was "truthy" or "falsy". The second ! flips it back to be a boolean which matches the original "truthy" or "falsy" evaluation.
var a = 'a string';
var f = !a; // f is now boolean false because a was "truthy:
var t = !!a; // f is now boolean true because a was "truthy:
It's not specific to Angular, it serves to transform a non-boolean value like undefined to boolean.

What is the usage of !! in JavaScript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the !! (not not) operator in JavaScript?
I just came across the code that uses !!, which means logical not not to me.
app.isArray = Array.isArray || function(object) {
return !!(object && object.concat
&& object.unshift && !object.callee);
};
What is the different between using !! and not using it?
(my guess is !! will convert the result to Boolean type. if my guess is correct, why is it so?)
!! Just inverts the content double into a boolean value. Here are some examples:
!! true === true
!! false === false

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.

Categories

Resources