Basic JavaScript - Testing Objects for Properties

Tell us what’s happening:

Why don’t the online compiler not allow the use of “.” as a notation for this question. While it allows the usage of “

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";
 }
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36

Challenge Information:

Basic JavaScript - Testing Objects for Properties

Hi there and welcome to our community!

I have linked an article which explains the difference between dot and bracket notation in Javascript.

Essentially, it comes down to the difference between static and dynamic keys.

EXAMPLE:

const myObject = {
  name: "igorgetmeabrain",
  age: 403,
  online: true
};

let prop = "online";

// use dot notation to access a static key
console.log(myObject.name); // igorgetmeabrain

// use bracket notation (in quotes) to access a static key
console.log(myObject["age"]; // 403

// using dot notation to access a dynamic key doesn't work
console.log(myObject.prop); // undefined

// using bracket notation (without quotes) to access a dynamic key
console.log(myObject[prop]); // true
2 Likes

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