stack overflow Flashcards

1
Q

Checking if a key exists in a JavaScript object?

A

Checking for undefined-ness is not an accurate way of testing whether a key exists. What if the key exists but the value is actually undefined?

var obj = { key: undefined };
obj["key"] !== undefined // false, but the key exists!

You should instead use the in operator:

“key” in obj // true, regardless of the actual value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly