Categories: Javascript Tutorial

What is Method and Difference between Functions and Methods in Javascript

Difference between Functions and Methods in Javascript

Before discussing the difference between a function and a method, we must understand, what is a function and what is a method. So, let’s understand a function first.

Function in Javascript

  • A function in JavaScript is a block of code, which is designed to perform a particular task.
  • It is a standalone piece of code that can be called from anywhere in the program.
  • We need to use function keyword, in order to define a function.
function greet() {
  console.log("Hello, world!");
}

greet(); // Calls the function

So, we can say that

  • functions can be declared independently.
  • functions can be called without being associated with an object.
  • functions are not tied to any object.

Now, Let’s see, what is a method

Methods in Javascript

  • A method is also a function, but it is always associated with either an object or with a class.
  • We have to write a method always inside an object or inside a class.
  • When we write a method inside an object, then we need to use function keyword while defining it.
  • When we write a method inside a class, then we can’t use function keyword while defining it.
  • So, a method is a funnction which is a property of an object or a class.

Definition – When a function is defined as a property of an object or a class, it is called a method of that object or the class.

Methods associated to an object

const person = {

  name: "Rajesh",
  greet: function() {
    console.log("Hello, my name is " + this.name + "!");
  }
};

person.greet(); // Calls the method

Methods associated to a class

class person {

  name;
  greet() {
    console.log(`Hello, my name is ${this.name}`);
  }

}

let obj = new person();
obj.name = "Rajesh";
obj.greet();

Difference between function and method

functionmethod (object)method (class)
We need to use function keyword while defining a functionWe need to use function keyword while defining a method associated to an objectfunction keyword should not be used when we define a method associated to a class
We can call function without using any object referenceWe have to use object reference for calling methodWe have to use object reference for calling method
Functions are general-purpose blocks of code that can be used independentlymethods are specific functions associated with an objectmethods are specific functions associated with a class
In a standalone function, this can have different values depending on how the function is invoked.In a method, this refers to the object to which the method belongs, allowing methods to access and modify the object’s propertiesIn a method, this refers to the object to which the method belongs, allowing methods to access and modify the object’s properties

Admin

Working Professional with more than 12 Years of Experience in Software Testing with Automation and Manual Testing Knowledge.

Recent Posts

Mostbet bahis sitesindeki kazancli oyunlar

Mostbet bahis sitesindeki kazancli oyunlar

55 years ago

Mostbet Turkey: Profesyonel Musteri Destegi

Mostbet Turkey: Profesyonel Musteri Destegi

55 years ago

Heyecan Verici Kazanclar icin Gates-of-Olympus Slot

Heyecan Verici Kazanclar icin Gates-of-Olympus Slot

55 years ago

Types of Objects in Javascript

In JavaScript, an object is a data structure that allows you to store collections of…

55 years ago

Objects, Classes and Constructor in Javascript

Objects, Classes and Constructor in Javascript Classes and Objects A class in any programming language…

55 years ago

OOPs in Javascript

We will start from object oriented programming basics like What are different concepts in Object…

55 years ago