JavaScript Object Methods

JavaScript Methods

JavaScript methods are the actions that can be performed on objects.

A JavaScript method is a property containing a function definition.

Property Value
firstName John
lastName Doe
age 50
eyeColor blue
fullName function() {return this.firstName + " " + this.lastName;}


Methods are functions stored as object properties.

Accessing Object Methods

You create an object method with the following syntax:

methodName : function() { code lines }

You access an object method with the following syntax:


You will typically describe fullName() as a method of the person object, and fullName as a property.

The fullName property will execute (as a function) when it is invoked with ().

This example accesses the fullName() method of a person object:


name = person.fullName();

Try it Yourself »

If you access the fullName property, without (), it will return the function definition:


name = person.fullName;

Try it Yourself »

Using Built-In Methods

This example uses the toUpperCase() method of the String object, to convert a text to uppercase:

var message = "Hello world!";
var x = message.toUpperCase();

The value of x, after execution of the code above will be:


Adding New Methods

Defining methods to an object is done inside the constructor function:


function person(firstname, lastname, age, eyecolor) {
    this.firstname = firstname; 
    this.lastname = lastname;
    this.age = age;
    this.eyecolor = eyecolor;
    this.changeName = function (name) {
        this.lastname = name;

The changeName() function assigns the value of name to the person's lastname property.

Now You Can Try:


Try it yourself »

JavaScript knows which person you are talking about by "substituting" this with myMother.

Color Picker