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 JavaScript Basics (Retired) Storing and Tracking Information with Variables Working with Strings and Finding Help

Pavle Lucic
Pavle Lucic
10,801 Points

Difference between object methods and object properies?

What is the difference between object METHODS and object PROPERTIES?

4 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

In the nutshell, Object in JavaScript is just key-value pairs stored in a Hash. The difference between property and method is that -- property is a value stored in the hash key, whereas method is a function stored in hash key.

var person = {
  name: "John Doe",
  sayHello: function() {
        console.log("hello");
  }
}

In this code sample.

  • name is the property of object person, it stored String "John Doe", you can access it via dot notation person.name.
  • sayHello is the method of object, and it is a function. You can also access it using dot notation person.sayHello(). Notice that sayHello is just a function, and function invocation requires you to use () here.
Pavle Lucic
Pavle Lucic
10,801 Points

This is a great explanation.

object.something; - it is object with propery; object.something(); is object method;

Pavle Lucic
Pavle Lucic
10,801 Points

I didnt think about that difference until i watched now video. Sometimes when i read documentation for implementing some new complex js plugin, if found dificult to understand in some parts when there is a mention about methods and function in the same time :)

William Li
William Li
Courses Plus Student 26,868 Points

yeah, method and property are among the fancy terms you often heard when doing Object-oriented programming in JavaScript, and they can be confused at first. But really, a method is nothing more than just a function stored in a Object; whenever you see code like this object.something(), you know you are calling a method of the object, or to put it another way, calling a function predefined in the object.

xenon thong
xenon thong
3,952 Points

Hi William,

Sorry for asking, but what is Hash?

William Li
William Li
Courses Plus Student 26,868 Points

hi, xenon.

Sometimes a Hash may refer to as Hash Table, dictionary, or associative array, they're all the same.

In the nutshell, a Hash is an essential data structure for storing key-value pairs, thereby enables you to retrieve the corresponding value from its key.

For more info on Hash, check out this article. cheers.

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Pavle Lucic

You can think of object properties like variables of the object, and methods like functions.

For example, a string has a .length property -- it's like a variable in that it holds a value, the number of letters in the string.

But the .toLowerCase() method is like a function, because it does something -- it takes the letters in a string and returns all of those letters in lower case.

Pavle Lucic
Pavle Lucic
10,801 Points

Hey Dave McFarland ,

Tnx on answer.

Is it length also function (variable that holds function). It takes the value from variable, counts the num of characters and returns it like sum of characters..?

I am little confused.

Dave McFarland
Dave McFarland
Treehouse Teacher

The .length property just stores the number of characters in the string. In that way, it's like a variable.

Colin Marshall
Colin Marshall
32,861 Points

From the Mozilla Developer Network:

An object is a collection of properties, and a property is an association between a name and a value. A property's value can be a function, in which case the property is known as a method.

Think of an object as an object in real life (such as a bicycle). A method is something you can do with the bike (pedal), or something you can do to the bike (re-inflate tire). A property is something about the bike: how many wheels it has (wheels) or its height (height).

Object: Bicycle Properties:

  • wheels
  • height
  • price
  • radius of wheel Methods:
  • repair
  • pedal

Hope this helps.