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 ;)
Related
This question already has answers here:
Test for existence of nested JavaScript object key
(64 answers)
Closed 16 days ago.
I'm trying to check to see if something exists in an array, let's say the array arr is:
{"targeting":{
"key1":"1",
"key2":"2"},
"session": "1234"
}
I want to check to see if arr.targeting.key1 exists so I do:
if (true === !!arr.targeting.key1)
This is true if it exists, but what if arr is empty? Then I get an error:
cannot read properties of undefined, reading 'key1'
What is the simplest way to check for the presence of arr.targeting.key1 that will just return false if arr.targeting or arr themselves are undefined?
Use optional chaining.
if (arr?.targeting?.key1)
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
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
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I check to see if an object has an attribute in Javascript?
I have a Javascript object defined as following:
var mmap = new Object();
mmap['Q'] = 1;
mmap['Z'] = 0;
mmap['L'] = 7;
...
How to check whether this map has a value for a given key (for example 'X')? Does .hasOwnProperty() get into play?
if ('X' in mmap)
{
// ...
}
Here is an example on JSFiddle.
hasOwnProperty is also valid, but using in is much more painless. The only difference is that in returns prototype properties, whereas hasOwnProperty does not.
You can use:
(mmap['X'] === undefined)
Fiddle: http://jsfiddle.net/eDTrY/