Basic JavaScript - Testing Objects for Properties

Tell us what’s happening:
Why dot (.) property is not working in this function…?

Your code so far

function checkObj(obj, checkProp) {
  // Only change code below this line
    // return obj[checkProp];
    return obj.checkProp;
    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/ Safari/537.36

Challenge: Basic JavaScript - Testing Objects for Properties

Link to the challenge:

Dot notation doesn’t work when you’re using variables. You can only use dot notation for a specific named property of an object.


Revisit the curriculum:

obj.checkProp would work if you had this object.

const obj = {
  checkProp: 'someValue'

console.log(obj.checkProp) // someValue

You do not have such an object.

checkProp is a parameter, which is just a variable. The variable contains a value and that value must be evaluated. obj[checkProp] will do just that and use the value stored inside the checkProp variable as the key for the object access.

const user = {
  name: 'John'

const key = 'name'

console.log(user[key]) // John

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