Code highlights logo




Methods are functions that are associated with an object. They can be accessed and invoked using the dot notation. Methods are useful for encapsulating behavior that is specific to an object and can be called upon whenever needed.

Creating Methods

To create a method, we define a function as a property of an object. This function can then be called through the object using the dot notation.

1const myObject = {
2 property1: value1,
3 property2: value2,
4 yourMethod: function() {
5 return this.property1 + ' ' + this.property2;
6 }

Invoking Methods

Once a method is defined, we can invoke it using the object's name followed by the dot operator and the method name, followed by parentheses.


The this Keyword

You may have noticed the `this`` keyword being used inside the method. In JavaScript, this refers to the current object that is calling or executing the method. It allows us to access the object's own properties within the method.



In the person object, add a method called sayAge that logs the age of the person.


Call the sayAge method to check if it logs the correct age.

Sign up to start coding

Already have an account?

Sign In

Course content