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

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

Related

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

How is the boolean statement evaluated in Javascript [duplicate]

This question already has answers here:
All falsey values in JavaScript
(4 answers)
Closed 3 years ago.
So i just found out [0] == true is false, and [0] === true is also false,
so i assumed [0] should be evaluated to false. But when I am writing
if([0]) {
console.log("hi");
}
prints "hi"
That happens because Boolean([0]) returns true. But i think it should have returned false.
What am I thinking wrong here?
JS engine interpretation is that it is a non empty array. Hence, a truthy value in If condition.

Why (thisIsInTheArray in array) returns false? [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)
Closed 5 years ago.
var codeList = [ "ok", "error" ]
var msg = { "status": "ok" }
console.log(msg.status in codeList) // returns false
It seems to be working on my iPad but not my computer. Here's a screenshot on my Chrome 64.0.3282.119, Windows 10 x64, JavaScript V8 6.4.388.40:
According to documentation:
The in operator returns true if the specified property is in the
specified object or its prototype chain.
Use array.includes() instead.
PS: There are a lot of interesting stuff in MDN documentation ;)

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

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.

Categories

Resources