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 Loops, Arrays and Objects Tracking Data Using Objects Accessing Object Properties

Prakhar Patwa
Prakhar Patwa
11,260 Points

I have a little doubt Regarding one concept which is "Method". In the video@5:50min. he mentioned the method.

var person = { name : 'Prakhar Patwa', country : 'India', age : 23, treehouseStudent : true, skills : ['JavaScript', 'HTML', 'CSS'] };

function print(message) { var div = document.getElementById('output'); div.innerHTML = message; }

var message = "<p> my name is "+ person.name + ".</p>"; message += "<p>I live in "+person.country+".</p>" person.name = "Prakhar Maheshwari"; message += "<p>My new name is "+person.name+".</p>" person.age += 1; message += "<p>My new age is now "+person.age+".</p>" message += "<p>My new skills is "+person.skills.length+"</p>" print(message);

// person is an object // name , country , age etc are the property or keys. // 'Prakhar Patwa' , "India" , 23 etc are the values /* then @5:40 in video he mentioned we can also use methos of that properties (in array). that part i am not getting it,what is method? if inside the array these are the methods?

please help me regarding this */

1 Answer

Nicolás Carrasco-Stevenson
Nicolás Carrasco-Stevenson
Courses Plus Student 5,668 Points

A method is a function associated to an object. For example .pop() and .unshift() are methods of the Array object and .floor(x) is a method of the Math object. Take a look at https://www.utexas.edu/learn/javascript/objects.html for a more thorough explanation on objects, methods and properties.

Now, to be more specific about your question @5:50, Dave McFarland mentions that you can use methods of the properties and gives the following example:

person.skills.join(', ')

What he is trying to say is that since our person object has a property called skills, wich happens to be an array, we can use all methods of the Array object, such as .join(), by simply using the syntax used above. I hope that helps