javascript - Difference between `x in y` vs `y[x]` -


using javascript, there difference in terms of performance or behavior between

let foo = {   a: true,   b: false,   c: true }  'c' in foo  ,  foo['c']  ? 

always wondered that, not sure how search answer. obvious difference foo['c'] return false if 'c' points falsy value, 'c' in foo return true if foo has key 'c'.

the reason ask question:

in many cases want store key (for quick search) , not care value:

 let foo = {    // keys important, values of no consequence       a: undefined,       b: undefined,       c: undefined   } 

i wondering if best practice store keys, without assigning value.

if(!('a' in foo)){     foo['a'] = undefined;  } 

it seems strange give hash values, when don't need use values, need store key.

you answer own question

the obvious difference foo['c'] return false if 'c' points falsy value, 'c' in foo return true if foo has key 'c'.

'c' in foo 

checks if there key named 'c' in foo

and

foo['c'] 

returns value


Comments

Popular posts from this blog

javascript - Replicate keyboard event with html button -

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Web audio api 5.1 surround example not working in firefox -