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

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.

Related

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.

How does the in operator work with sets in JavaScript? [duplicate]

This question already has answers here:
In operator issue in JavaScript
(4 answers)
Why does javascript's "in" operator return true when testing if 0 exists in an array that doesn't contain 0?
(6 answers)
What is the difference between "in operator" and "includes()" for JavaScript arrays
(4 answers)
Closed 1 year ago.
Why does the following code:
'%' in new Set('%');
return false?
And is the in operator faster on a set than on an array?
The in operator returns true if the specified property is in the specified object or its prototype chain.
It does not check if something belongs to a set. You should use Set.prototype.has for that.
const obj = { someKey: 'someValue' };
'someKey' in obj; // true
'someOtherKey' in obj; // false
const set = new Set([1,2,3]);
set.has(1); // true
set.has(4); // false
Regarding performance, please refer to this question

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

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

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

Categories

Resources