Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

bracket notation works but dot notation doesnt work

// Setup
var myObj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh"
};

function checkObj(checkProp) {
  // Your Code Here
 if(myObj.hasOwnProperty(checkProp)) {
   return myObj[checkProp]                   <----------------Right here dot notation doesnt work 
                                                                                        why is that?
 } else {
   return "Not Found"
 }

}

// Test your code by modifying these values
checkObj("gift");
Rich Donnellan
Rich Donnellan
Treehouse Moderator 27,671 Points

Your question/answers/comments have been updated with code formatting. Check out the Markdown Cheatsheet below the Add an Answer submission for syntax examples.

2 Answers

Steven Parker
Steven Parker
229,608 Points

Since the function is passed a property name as a string argument, there's really no opportunity to use dot notation.

Dot notation is what you use to access a property using the literal property name that you know in advance (like "MyObj.bed"). But to access a property using a name from a variable, bracket notation is the only option.

Steven Parker lets say that when i called the function i input text not a string in the place of value will it be converted to a string because of typecasting?

  let updateRecords = (id, prop, value) => {
  if(prop !== "tracks" && value !== "") {
    collection[id][prop] = value ; 
  }

  if(prop === "tracks") {
    collection[id][prop] = [ ]
    collection[id][prop].push(value) ;
  }

  if(prop === "tracks" && value !== "") {
    collection[id][prop].push(value) ;   
  } 

  if(value === "") {
    delete collection[id][prop] ; 
  }
}
Steven Parker
Steven Parker
229,608 Points

I'm not sure what you mean by "text". When is "text not a string"?

I see your examples have several words in quotes, but those are string literals. Not variables, but still strings.

Steven Parker by text i mean just a word.

collection[id][prop].push(value) ; <- look at this if i entered hello in the spot of value when i called the function would the word be converted into a string? you cant push a word into an array

Steven Parker i dont have a computer with me right now i cant check

Steven Parker
Steven Parker
229,608 Points

I'm still not sure what you mean by a "word". If it's not in quotes, it must be a variable's name, which stands for whatever the variable was assigned with. If it is in quotes, then it is a string literal.

Either way, you can push it into an array. What would make you think you can't?