Difference between functions and methods in JavaScript
Tags: webdev, javascript, beginners, tutorial
Published: 2023-01-26 07:00:00

When you start learning functions and methods in JavaScript you might use these words as if it's the same because they might sound and look similar sometimes however functions and methods are different things.

I will try to explain in a very simple way the difference between functions and methods in JavaScript.

![Definition](https://media4.giphy.com/media/wpvrvjjDf7uRoM61w5/giphy.gif?cid=ecf05e47f08mo6ovzgxultgow71bvl60aoldyve35yihh0dk&rid=giphy.gif&ct=g)

## Definition

A function is a block of code that is written to perform specific tasks. 

A method is a property of an object and contains a function definition. In other words, a method is also a function but stored as an object property.



![Syntax](https://media1.giphy.com/media/Q0OqU2N1MN3KU/giphy.gif?cid=ecf05e47kld5cpao51gnxdgreueorlbgi4ah9tlxsmfzste5&rid=giphy.gif&ct=g)
## Syntax

A function is defined by using a keyword function followed by a name we come up with. The body is enclosed in curly braces. 

![Function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7i88jbvg6q1wr10baz1a.png) 


A method is located inside objects and we access it by accessing the object first and then the method. There are types of methods that already exist in JavaScript, for example, strings or arrays already have built-in methods. Though you can create them yourself as well.


![Method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yp92ofy49wmcru08cfq0.png)




![Existence](https://media0.giphy.com/media/l1IYav3XvHjYOF2yA/giphy.gif?cid=ecf05e47c0fmi9kugvk15kbzqkccy9qpr7j320mzpb3hu1ve&rid=giphy.gif&ct=g)
## Existence

A function can exist on its own and does not have to be attached to anyone. However, at the same time, it actually is attached to something and that is a global object. In any case, it's still considered a function.

A method needs to be attached to objects and needs to be someone's property with a function value. It cannot exist on its own.

![Calling](https://media0.giphy.com/media/xT5LMzhyrAku8HzaQE/giphy.gif?cid=ecf05e47p0jl3xz11iuxh0n3u7dy03ahq7qmopzrbkw41eza&rid=giphy.gif&ct=g)
## Calling

To call a function you can use various ways and one of them can be calling the function with its name or it can even call itself(self-invoking function).

![call a function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2d88251zahe1tys52b32.png)


To call a method you need to use the object name first and separate it from a method name with a dot.


![call a method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9g8o9m8br8kmdve60kne.png)






To summarize, in JavaScript, a function is a block of code that can be defined and then invoked, while a method is a function that is associated with an object. The key difference between the two is that a method has access to the properties and methods of the object it is associated with, while a function does not. Understanding the difference between functions and methods is important because it allows developers to use the appropriate tool for the job, and can help improve code organization and readability. Additionally, understanding the difference can also help prevent common programming errors, such as attempting to use a method as a function or vice versa.


**📌 Enjoyed the post? Please let me know in the comment down below!**