JavaScript

What is a method in JavaScript?

The simplest definition of a method is that it is a function that belongs to some class. However, in JavaScript, a method is a function that belongs to some object or a set of some instructions that performs a certain task. A function is a code of some instructions which performs a task. An object is a thing with certain properties and types. A shirt, for example, is an object, and its color or size are its properties.

Method Invocation Syntax

myObj.myMethod('Argument');

Where myObj is an object on which the myMethod is called.

What is a method?

A Javascript method is an action done on an object, and it is a property that holds a function definition, for example, suppose you have a function that has firstName, lastName, regno, and a method with the name of fullName that returns the first name and the last name of a person i-e the full name of a person. The fullName is the property and the function is the value.

const student = {
  firstName: "Jhon",
  lastName: "Cena",
  Regno: 12311,
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
};

alert(student.fullName());

The fullName() is the method of the student object and acts as a property. The fullName is executed like a function when invoked with () however it should be kept in mind that it is a property.

We used this keyword in the above example because we wanted to access the property of an object within a method of the same object.

Suppose we invoked the fullName without the parenthesis () then it will simply return the function definition:

const student = {
  firstName: "Jhon",
  lastName: "Cena",
  Regno: 12311,
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
};

alert(student.fullName);

Method Addition to an Object

We can also add a new method to an object in JavaScript easily. We will change the above example code slightly to achieve our purpose i-e we will add a method to an object:

const student = {
  firstName: "Jhon",
  lastName: "Cena",
  Regno: 12311,
};

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

alert("Student Full Name: " + student.name());

Built-In JavaScript Methods

Built-in methods or functions are predefined pieces of code in a program or programming framework or a programming language that performs some specific task. This makes programming easy as programmers don’t have to create a new method or function and can simply directly use the built-in methods in their application. Like any other programming language, JavaScript also offers some Built-in methods for example:

let name = "Jhon Cena";

alert(name.toUpperCase());

In the above example, we declared a name and then alert that name by using the inbuilt method toUpperCase() and we can see in the screenshot above that the name was converted to upper case. Apart from the toUpperCase(), there are also numerous built-in methods that we can use, for example:v

  • Date()
  • Date.now()
  • Math.round(num)
  • Math.floor(num)
  • string.length()
  • string.toLowerCase()
  • Array.length
  • array.push()

Function and Method Difference

This topic is always a confusing one however we will today clear all your doubts by defining the key differences between a function and a method.

Function Method
The function is a set of code that performs some tasks. A method is also a function but it is associated with an object.
Can be called directly with the use of its name Can be called with the object name or method name
Can pass some data and after some operation can return the data The data is operated in a class
Data is explicit that is passed to a function Implicitly passes the object which was called
Lives on its own Associated with an object property

Conclusion

A method is simply a function that associates to a class, and that class belongs to an object in JavaScript. Methods are extremely useful as they can make a programmer’s life easy because methods are reusable, less coded, and can be easily implemented.

In this short tutorial, we looked at what a method is in JavaScript and how it is implemented, as well as what JavaScript built-in methods are and the differences between a function and a method were discussed in the end.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.