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 Foundations Arrays Methods: Part 2

Paul Son
Paul Son
5,474 Points

Methods and functions

What is the difference between methods and functions? When should you use them?

2 Answers

Tom Mertz
Tom Mertz
15,254 Points

Methods and Functions are pretty much the same thing. They should be used when you have a task that you want to be able to repeat, but not have to rewrite the code over and over again. Instead you would just call the method/function to handle the task. For example, if you need to calculate a value multiple times using the same expression.

A method is just a function that is associated with an object. So if you have a function that belongs to an object, it would be called a method of that object.

So, function - a block of code that can be repeated by calling it.

Method - a function that belongs to an object and describes some behavior of that object.

Good luck!

Paul Son
Paul Son
5,474 Points

Awesome. Thanks!

Dennis Castillo
Dennis Castillo
16,018 Points

The "FUNCTION" can be called directly when an event occurs (like when a user clicks a button), and it can be called from "anywhere" by JavaScript code.

A "METHOD" is just an object property that happens to be a function.