Back to questions
JuniorTheoryCommon
What does the program print?
const key = "a";
const o = { a: 1, key: 2 };
console.log(o.key, o[key]);2 1
- Expecting
o.keyto use the variablekey: after a dot the name is taken literally, soo.keyreads the property namedkey, which is2. - Expecting
o[key]to read the property namedkey: inside brackets the expression is evaluated first, soo[key]iso["a"], which is1.
- How would you read the property
keyusing brackets? - What does
o.bprint, given that the object has no propertyb?