Checking for a key in an object correctly
by Lucas Krause
JavaScript
var a={};
//Exactly one of the following 2 tests must be true
console.log(0 in a); //we expect 'false', and we even get 'false'
/**
* logically this must be true because we test whether an item with the key '0' doesn't exist in `a`, and it doesn't.
* But we get 'false' because the notation is incorrect. The line doesn't test whether the item '0' doesn't exist in `a` (as meant by us)
* but it switches the '0' to 'true' as a consequence of using the NOT operator ('!') and this doesn't exist in `a`, too.
* The next line clarifies what happens in the line below this comment block.
console.log( (!0) in a )
*/
console.log(!0 in a); //returns 'false'
//To get the result that we expect, we have to use the following line
console.log(!(0 in a)); //returns 'true' because the item '0' isn't included in `a`