This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 9 years ago.
In JavaScript, what does the '!!' operator do?
Is it a NOT NOT statement?
For example:
someFunc(foo) {
return !! foo;
}
// Return foo only if foo exists?
First this is not an operator.converts it to boolean in javascript
EXAMPLE:
var test = true;
!test = false; //It will converted to false
!!test = true; //Again it will converted to true
! is the "not" operator, casting its one argument to a boolean and negating it.
The second ! negates it back, so effectively !! casts the value to a boolean.
Converts foo to boolean.
var foo = "TEST";
!foo // Result: false
!!foo // Result: true
Related
This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 2 years ago.
Can someone point me in the way towards some documentation as to what it means to use !!.
I have a return statement
return !! _.reduce(collection, function(a, b){
// a is a result of last call
// b is result of calling iterator on current value
console.log("a: "+a)
console.log("b: "+b)
console.log(iterator(b));
//will return false or true based on test
return a && iterator(b);
}, true)
}
which works but this doesn't
return _.reduce(collection, function(a, b){
// a is a result of last call
// b is result of calling iterator on current value
console.log("a: "+a)
console.log("b: "+b)
console.log(iterator(b));
//will return false or true based on test
return a && iterator(b);
}, true)
}
!! is shorthand in Javascript for converting any value to its boolean equivalent.
The way it works is that a single ! operator converts any value to its boolean opposite. A second ! then converts it back to its original boolean value, because the opposite of the opposite (in boolean terms) is always the original.
This question already has answers here:
Why don't logical operators (&& and ||) always return a boolean result?
(9 answers)
Closed 5 years ago.
There is one interview question below.
The logical AND of two truths should be true. But the output is 3. Why?
var a = 2;
var b = 3;
var c = a && b; // value of c = 3
console.log(c);
Check this out.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
If you use && with non-boolean values, it returns the first element if it can be converted to false. If it cannot converted to false, it returns second element
Returns a if it can be converted to false; otherwise, returns b.
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.
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does !new Boolean(false) equals false in JavaScript?
var b = new Boolean(null);
alert(b instanceof Boolean);
if(b) {
alert('cv');
alert(b.toString());
}
Why if code block is executed? Is b supposed to be a boolean type and evaluated as false?
Please explain thanks
The code block executes, because the object exists and is not undefined although it has no value currently. The point of the Boolean object in javascript is to convert non-boolean objects to the "true" or "false" value.
if you have
if( b.valueOf() );
that will evaluate the actual value of the object.
All objects are truthy, except null. Therefore, even if you write new Boolean(false) specifically, it will still be truthy.
This is why you never write new Boolean. To cast to a boolean, just use !!