I don’t understand when to put double quotation marks on property while using the bracket notation or dot notation.
In my code, it doesn’t work when I put double quotation marks and I works when I don’t.
Your code so far
function checkObj(obj, checkProp) {
// Only change code below this line
if (obj.hasOwnProperty("checkProp")) {
return obj["checkProp"];
} else {
return "Not Found";
}
// Only change code above this line
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36.
checkProp is not a property name. It is a parameter to checkObj function. If you invoke the function, passing in a string which is a property of obj, the passed string will replace the parameter. checkProp is like a variable.
In a way, you’re asking the difference between a variable name and a string.
Use this example:
const planets = {earth:true, mars:true, pluto:false}
//access property using a string
console.log(planets["mars"])
//access property using a variable
let usingVariable = "mars"
console.log(planets[usingVariable])
When you input a string, nothing is replaced.
planets["mars"] will be that, and only that forever.