I learned in the previous challenges that in bracket notation you have to use quotes if there is space between words in the property name. However, I found that you have to use quotes even if there is no space.
But, in this particular challenge, in the syntaxes below the solution was only accepted when I removed the quotes from checkProp from both of them:
obj.hasOwnProperty(checkProp)
return obj[checkProp];
Can anyone please explain it to me? When and when not to use quotes in bracket notations?
Now, bracket notation could be used always, but there are two cases where you must use it - if the property name is stored in a variable or if the property name is not a valid JS identifier (e.g., it has a space in it).
const obj = { foo: 'bar' };
const checkProp = 'foo';
console.log(obj.checkProp);
// undefined
console.log(obj[checkProp]);
// bar
console.log(obj.foo);
// bar
console.log(obj['foo']);
// bar
console.log(obj[foo]);
// Uncaught ReferenceError: foo is not defined