Skip to content

What's the difference between a method and a function?

New Course Coming Soon:

Get Really Good at Git

What is a method? And what is a function? What's the difference?

A function lives on its own:

const bark = () => {
  console.log('wof!')
}

bark()

or

function bark() {
  console.log('wof!')
}

bark()

A method is a function assigned to an object property:

const dog = {
  bark: () => {
    console.log('wof!')
  },
}

dog.bark()

The method can access the object properties, but only when it’s a regular function, not an arrow function:

const dog = {
  name: 'Roger',
  bark: function () {
    console.log(`I am ${this.name}. wof!`)
  },
}

dog.bark()
→ Get my JavaScript Beginner's Handbook
→ Read my JavaScript Tutorials on The Valley of Code
→ Read my TypeScript Tutorial on The Valley of Code

Here is how can I help you: