Object property call

function checkObj(obj, checkProp) {
  if (obj.hasOwnProperty(checkProp)) {
    return obj[checkProp];
  } else {
    return "Not Found";

Please why can’t this

return obj.checkProp; 

be used in place of

return obj[checkProp];

I have edited your code for readability. When you want to post code on the forum, please enclose it between two sets of triple backticks, so that it displays legibly.

You can type the backticks by hand or use the Preformatted Text Tool (</> icon or CTRL+e) to create them automatically.

You have raised an important point about accessing object properties using dot and bracket notation.

Essentially, dot notation only allows static keys, whereas bracket notation allows for dynamic keys (e.g. variable names).

This article explains it better than I can:

Thanks this was helpful

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.