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

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.

Related

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-negation + logical and in Javascript [duplicate]

This question already has answers here:
Why don't logical operators (&& and ||) always return a boolean result?
(9 answers)
Closed 7 years ago.
I just ran across this code
var someBool = !!(o.a && o.a.t);
I was about to remove the double-negation, then realized it forces someBool to be a boolean value... Looking on the MDN, I find this example
a5 = "Cat" && "Dog"; // t && t returns "Dog"
This seems atypical based on my experience in other languages. I'd expect the logical and operation to always return a boolean value. Can anyone explain why this use case is supported in Javascript?
Also, is the code that sent me in this direction the best way to force a bool around logical and? I'm aware of new Boolean, but that doesn't return a primitive type like the double-negation, so perhaps that's the way to go?
The && and || operators have an effect similar to boolean AND and OR, but they don't affect the expression value. The expressions are evaluated and the values tested with the JavaScript "truthiness" rules, but the overall expression value is that of the actual operand expressions (well, one of them).
Yes, that's different from C and Java and etc. JavaScript is a different language with its own rules.
The Boolean constructor can be called without new to perform a "truthiness" test and return a boolean primitive. Using !! is fairly idiomatic however.

What does the !! operator do? [duplicate]

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

Javascript Boolean type [duplicate]

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 !!

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

Categories

Resources