5 ways to declare a function in JavaScript
Tags: webdev, beginners, javascript, tutorial
Published: 2022-12-01 19:34:07

JavaScript functions are used to perform actions when certain events occur. They are also called methods.

A JavaScript function is usually a block of code that contains a group of instructions to execute.

There are many ways to declare a simple function in JavaScript. In this tutorial, we will explore them with some examples.

## 1)- Ways to declare functions
**a)- Function declaration**
Also called a function statement or a function definition, is the most common way of declaring a function in JavaScript.

The function declaration method simply means that we will declare the function using the keyword function followed by the name of the function itself like below:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xgqfhg7laks61wy36q3f.PNG)

So here “**function**” is the keyword, and “**course**” is the name of the function.

If we want to call the function all we have to do is write its name followed by the parenthesis like below:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ggflqxqx9lri76gp6dd9.PNG)

A function can take arguments. If we have dynamic data, we can pass the function as many parameters as we want.

Let’s say that for function **course()** whenever we call it we want it to display “**Welcome to this tutorial**” plus the name of the student.

The name of the student can change, as we can have 10 or 20 or 30 students….

So to do that we will pass a parameter called “**studentName**” to the function.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6syje82m0gl0sylj3kll.PNG)

So now for the function to display what we want, at the moment when we will call it we should pass it the name of the student.

Let’s say that the student’s name is Thomas.

So the code will be:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4bxk5f0bbdcd4asfp6z7.PNG)

**b)- Anonymous function**
When a function is declared anonymously, it means that the function does not have any name.

Unlike in the general declaration function where a name is given to a function, in the anonymous function there is no name.

An anonymous function is declared using the **function** keyword only, like below:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rg56b5tqeklxyn7u7dve.PNG)

As you can see, the function does not have any name. That’s why it is called anonymous.

However, as an anonymous function is not accessible after its initial creation, and as it can only be accessed via a variable, we will store it in a variable that we’ll call course, and this is what we’ll see in the next paragraph.

**c)- Function expression**
The function expression method allows us to create an anonymous function that doesn’t have any function name.

Below is an example of an anonymous function expression. let’s see the code:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vtnknmnw51rduymb6yd8.PNG)

Here the variable **course** stores an anonymous function.

So the anonymous function is invoked by calling out the variable with trailing parenthesis and semicolons.

The code below will call the function:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yrhx6neawgx9kvcd1mbv.PNG)

**d)- Arrow function**
This method is a cleaner way of creating JavaScript functions.

So instead of declaring a function like this:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/13py98jnh3fwg2gttscc.PNG)


We can declare it like this:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xze3robm70vc5qae4hkp.PNG)

However, as the arrow function does not have a name, it should be stored in a variable if we want to invoke it later, just like the function expression.

So the code will be:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hymazpq277pv7wueby8s.PNG)

Arrow functions provide a concise syntax for defining anonymous functions. An arrow function expression has a shorter syntax compared to other ways of declaring functions.

An Arrow function can also have arguments like this:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iqjyimpceg8segot1z0o.PNG)

When creating an arrow function, the parenthesis pairs and curly braces are optional for a single function parameter and a single statement.

If there are many instructions that the function should execute, then these instructions should be wrapped inside curly braces:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4mq7kspis1fys6pu4hxa.PNG)

**e)- Constructor function**
Another way to declare a function is by using the Function constructor with the new keyword.

Let’s have a look at the syntax first:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eagtyyrq49yfqwf04zxs.PNG)

JavaScript has a built-in constructor Object called Function that can be used to declare and create functions.

This constructor can take any number of arguments. For this example we passed it 2 arguments which are “**a**” and “**b**“.

The last parameter is where the instructions that the function is supposed to execute should be added. It is there where goes the function body.

The general syntax is:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/igjfyxm7l9cb2bzksw91.PNG)

So in this example we created a function using the Function object, its name is sum().

## 2)- Which way is better?
There is no answer to this question because all depends on the situation and the function code.

You can use any method or function type you want and all of them will give you the same results.

so if you’re comfortable with either approach, feel free to pick whichever you prefer. However, I’d recommend sticking with the arrow function type whenever possible, as it offers better performance and readability.

## Read More
[Object constructor functions: How does the Javascript constructor work?](https://purpose-code.com/javascript-object-constructor-function/)

[Send form data received in the backend to the database (MongoDB)](https://purpose-code.com/send-form-data-to-database/)

[Send form data to the back-end (Node JS)](https://purpose-code.com/send-form-data-to-the-back-end-node-js/)

[Get the data back from the database (MongoDB) and display it on the front-end](https://purpose-code.com/get-the-data-back-from-the-database-mongodb-and-display-it-on-the-front-end/)

[How to send data from back-end(Node JS) to MongoDB database?](https://purpose-code.com/send-data-from-backend-node-js-to-mongodb/)