As you learned in earlier modules, functions are reusable blocks of code that perform a specific task:

function greet(name) {
    return "Hello, " + name + "!";
}
console.log(greet("Alice")); // "Hello, Alice!"

Object methods, on the other hand, are functions that are associated with an object. They are defined as properties of an object and can access and manipulate the object’s data. Here’s an example of an object with a method:

const person = {
    name: "Bob",
    age: 30,
    sayHello: function() {
        return "Hello, my name is " + this.name;
    }
};
 
console.log(person.sayHello()); // "Hello, my name is Bob"

In this example, sayHello is a method of the person object. The this keyword allows the sayHello method to access the properties of the object named person. You will learn more about the this keyword in future lectures.

A difference between functions and methods is how they are invoked. Functions are called by their name, while methods are called using dot notation on the object they belong to. For example, we call the greet function as greet("Alice"), but we call the sayHello method as person.sayHello().

Another important difference is the context in which they operate. Regular functions have their own scope, but they don’t have a built-in reference to any particular object. Methods, however, are bound to their object and can access its properties and other methods using the this keyword.

A key point to note is that, methods help in organizing code into logical objects, while functions are used for more general, reusable code.

Understanding the difference between functions and object methods is important as you progress in your JavaScript journey. While they may seem similar at first, recognizing when to use each will help you write more organized, efficient, and better code.