Methods in JavaScript

In JavaScript, however, a method is a function that belongs to an object. A JavaScript method is a property containing a function definition.

Few Important things of Methods:-

  • In JavaScript, however, a method is a function that belongs to an object.
  • JavaScript methods are the actions that can be performed on objects.
  • A JavaScript method is a property containing a function definition.
  • Accessing the object method is done with the following syntax: objectName.methodName()

Just similar to the functions in JavaScript methods are also classified in two categories:-

  1. Predefined
  2. User-defined

Predefined methods can be used directly by calling and giving parameter values (if required). while user-defined methods are first declared, defined, and then used in the program.

Deceleration and definitions of User-defined methods are done together we could understand better seeing the below code.
Syntax of Methods

object = {
    methodName: function() {
        // Content
    }
};

object.methodName()

How to access the Object Methods

we can access the object method with the following syntax stated below:-

objectName.methodName()

How to add the method to Object.

person.name = function () {
    return this.firstName + " " + this.lastName;
};

We could also directly use the built-in method.

var message = "Display message of your choice";
var x = message.toUpperCase();

The above method would just convert the text entered as a message to uppercase.

Few important links for learning:-

  1. Want to know the difference between Function and Method in JavaScript? Just click here.
  2. Want to know what is function and how to use them in JavaScript? Just click here.
  3. Want to know what are variables and how to use them in JavaScript? Just click here.
  4. Let’s see an example for User form validation in JavaScript. click here.
  5. Let’s understand JavaScript. what and how? click here.
Tagged : / / / / / /