Javascript if statement double exclamation mark operator redundancy [duplicate] - javascript

This question already has answers here:
Why use if (!!err)?
(3 answers)
Closed 5 years ago.
If I have an if statement that doesn't have a conditional operator, does it matter if I use the double exclamation mark operator - !!?
For example...
if ([]) {
}
vs...
if (!!([])) {
}
From what I understand - the operator is basically asking - "is this value truthy?". So it is redundant in this case. Is it redundant in all cases similar to this?
EDIT:
if (x) { console.log("hi"); }
if (!!x) { console.log("hi"); }
Will both of these print for any x? That is my question.

Yes. Both of these will print for any Boolean(x) === true
For more detail, you can find truthy, falsy and Boolean in JavaScript
For esier to understand, you can use Boolean([]) // true

Related

What is going on with the 'in' function in javascript? [duplicate]

This question already has answers here:
Why does javascript's "in" operator return true when testing if 0 exists in an array that doesn't contain 0?
(6 answers)
Why does ("a" in ["a","b"]) yield false, and (1 in [1,2]) yield true? [duplicate]
(1 answer)
Closed 3 years ago.
Something that recently broke my code is that I naively thought that:
'+' in ['+','-',...] = true.
The only problem is that it actually evaluates to false!
Someone please help me understand what is going on here!
The in operator returns true if the specified property is in the
specified object or its prototype chain.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in

Exclamation mark before an object? [duplicate]

This question already has answers here:
What does "!" operator mean in javascript when it is used with a non-boolean variable?
(5 answers)
What is an exclamation point in JavaScript?
(2 answers)
Closed 5 years ago.
Coming from java, learning angular/typescript: The ! negates booleans or boolean expressions. This is, according to my research, the same in javascript/typescript. But what does the following method do?
isSelected(product: Product): boolean {
if (!product || !this.currentProduct) {
return false;
}
return product.sku === this.currentProduct.sku;
}
What is !product? Product is coming in as a method parameter and then it is asked, if product is not product, or wtf should that mean? :D

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.

What does "|| [];" mean? [duplicate]

This question already has answers here:
JavaScript OR (||) variable assignment explanation
(12 answers)
Closed 6 years ago.
What does || [] mean in the code below? Why might this be needed?
getPair: function() {
return this.props.pair || [];
},
[] is an empty array. ||, in this context, is a guard operator.
The statement says to return this.props.pair, but if this.props.pair is falsy, an empty array will be returned instead.

Javascript - Why use a double negation [duplicate]

This question already has answers here:
Why use if (!!err)?
(3 answers)
Closed 8 years ago.
Within an if condidition, when does it make sense to use a double negation? I found this pattern several times:
JS
if(!!children.length) { ...}
It's a common method to turn a normal expression to a boolean. It doesn't really make any sense inside an if statement, since it evaluates the given expression as a boolean anyways, but is useful outside.
For example:
const isEmpty = !!children.length;

Categories

Resources