Exclamation mark before an object? [duplicate] - javascript

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

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

Why use '{' and '}' to define a javascript variable [duplicate]

This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
What does this symbol mean in JavaScript?
(1 answer)
Closed 4 years ago.
I can't seem to find anything in the documentation to understand why the { and } characters are wrapped around variables on some examples I've seen. Can anyone example why we would do that?
For example:
const {app} = require('./app/app.js');

Javascript if statement double exclamation mark operator redundancy [duplicate]

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

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