Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Object-Oriented JavaScript (2015) Introduction to Methods Object Literal Review

Sam Parkash
Sam Parkash
6,645 Points

object literals vs properties

Hi,

Can someone please explain to me the difference and benefit of using an object literal over a property? From my understanding so far they are the same thing except object literals just let you get away with having a space in your property name or something else that is usually invalid.

Sam Parkash Thanks for asking this. I was wondering the same thing.

3 Answers

Julie Myers
Julie Myers
7,627 Points

An object literal and a property are two completely different things. An object literal is one way of creating an object, and an object has methods and properties. A property is a part of what makes up an object. For example:

//This is creating an object using object literal technique:
var myObject = {
  name: "Alice",
  age: 18,
  getName: function() {
       console.log(this.name);
   }
}

/*
name and age are both properties of the object myObject.
getName is a method of the object myObject.
*/

There are other ways of creating an object such as using the constructor function with the new keyword.

Julie Myers
Julie Myers
7,627 Points

You're welcome. :)

Jeremy Castanza
Jeremy Castanza
12,080 Points

Sam, I think that your question was just missing some of the correct terminology. However, Julie provided a good explanation for the way that the question was worded.

I think you might have been getting at the difference between dot notation versus square bracket syntax - when it comes to calling a key value pair for a property or calling a method.

Ultimately, dot notation is probably more widely used and preferred

myObject.name

Rather than square bracket syntax...

myObject["name"]

I think what Andrew was getting at is that there's more than one way to get your property information. This could be useful if let's say that I built an object like this and you're trying to access my object for your own program. For example:

var myObject = {
  "Aren't I cool?": "Alice"
}

If I built my object this way, you would be forced to change the key for my object. Changing other people's code is always a dicey proposition since you'll have to keep doing it upon updates. Otherwise, you could access the property by using square bracket notation like this:

myObject["Aren't I cool?"]

Hope this helps!