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

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

Related

how to check that a variable contains something in JavaScript [duplicate]

This question already has answers here:
if else statement reverse
(4 answers)
Closed 12 days ago.
Good morning
there are many tutorials that explain how to check if a string is empty but not the other way around
so i would like to do this
if variable not empty then we do this
if not empty then we do this
//do the opposite
if (nomvalidation === null || nomvalidation.trim() === ""){
}
Simply change the equality operators to inequality and the or to and and.
if (nomvalidation !== null && nomvalidation.trim() !== ""){
}
You may also want to look into the typeof operator to make sure its a string and undefined as another possible "empty" state.

How to simplify simple statement in typescript or js [duplicate]

This question already has answers here:
Understanding JavaScript Truthy and Falsy
(9 answers)
Closed 5 months ago.
I would like to simplify this expression but don't know how. There has to be a way to have the same expression without the three conditions.
if ( request.Document != null &&
request.Document != undefined &&
request.Document != "" )
Just for context I am talking something similar to c#'s !string.isNullOrEmpty()
if (request.Document) {
...
}
as null, undefined, '' will all return false.
(but [], {} or ' ' will return true)
Check the equality table for more info about other values.

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.

Why would I use !! in JavaScript? [duplicate]

This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 8 years ago.
While reading lodash source code, I saw:
this.__chain__ = !!chainAll;
Why would one use !! on the chainAll parameter?
I assume this is a safer way to detect falsy values or dealing w/ different JavaScript versions, but would like to know the scenario it protects.
The !! construct is a simple way of turning any JavaScript expression into
its Boolean equivalent. For exmaple: !!"something" === true, while !!0 === false
DEMO
JS use dynamic type for variable. Thanks to this trick you convert the variable into a boolean if it was not a boolean, an it doesn't modify the value if it was a boolean

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.

Categories

Resources