DEV Community

Lukas Polak
Lukas Polak

Posted on

Difference between function and method keywords

A method is a function that belongs to the object. Methods have a "receiver" while functions do not.

const someObject = {
  someMethod: function () {
    console.log('Hello, World!');
  },
};

// method call
someObject.someMethod(); // `someObject` is a receiver, `someMethod` is a method

const someFunction = someObject.someMethod;

// function invocation
someFunction(); // JavaScript functions are invoked without a receiver, using parenthesis syntax

function helloWorld() {
  console.log('Hello, World!');
}

helloWorld(); // logs "Hello, World!"

// When a function is declared on the global scope, it becomes a property of a global object
// The `window` is the receiver of `helloWorld,` which would make `helloWorld` a method and not a function. However, in JavaScript, it is still considered a function.
window.helloWorld(); // logs "Hello, World!"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)