The function is the key fundamental of programming languages, it is used to combine different expressions and instruct them to perform certain operations.
A function makes the overall structure of the program better by providing a feature to group certain expressions and operations, which also helps in reducing duplicate code.
Functions in Node.js
A function is the core of NodeJS, NodeJS uses functions everywhere such as for standard operations, as an arrow function, or as a callback function.
A function involves two parts
- Function Definition
- Function Calling
How to Define a Function in NodeJS?
Function Definition means defining the function behaviour, functionality, name, parameters, and the value it returns. In other words, before using a function it is required to create that function, and creating a function is called the function definition.
Syntax of a function
function function_name (parameters) {
// function_body
}
- function: It is important to write the function keyword in order to create a standard function in Node.js
- function_name: The function name is used to call the function. Function names can contain letters, digits, underscores, and dollar signs
- parameters: We pass some parameters during the function call and the actual function performs the operation on that parameters.
- function_body: A function body contains the actual operation definition which defines what a function can actually do.
- return: a return statement is used to define the value a function returns when calling.
Let’s see a quick example to understand the function definition.
function sum (x, y) {
return x + y;
}
Here we have a function of name sum, which take two parameters, x and y, and inside the function body, we define that this function when calling returns the sum of both parameters.
Calling a Function
The function we define will execute when it is invoked. The invoking of a function is called a function call.
Syntax
function_name(argumenst);
- function_name: The function name is the name of the function you want to invoke.
- argument: The argument is the value you want the function to perform certain operations.
const output = sum(2, 3);
console.log(output);
Here we are calling the function which we created earlier, and we pass the numbers 2 and 3 as arguments so that our function definition will perform the sum operation on that numbers and return the actual sum. Then we will set the value of the sum to a constant output and log its value.
Output
5
Different types of functions in NodeJS
- Arrow Function
- Function Expression
- Callback Function
Arrow Function
An arrow function is a special type of function which we can create and use without using the function keyword. We can even remove the curly brackets if the function body is the single liner and also the round brackets around the parameter if the number of parameters is one. In the arrow function, we use the arrow symbol instead of the function keyword.
Syntax
Let’s create an arrow function that takes the name as a value.
const function_name = (parameters) => {
// function_body
}
Since the function body contains the single-liner expression so we can remove the curly brackets, also the number of parameters is one so we can also remove the round brackets.
const function_name = parameter => // function_body
Calling an arrow function is the same as calling a traditional function.
Function Expression
We always use the function calling to call a function, but if it is required to call a function immediately when it is defined then we can use function expression.
Syntax
(function (parameters) {
// function_body
})()
Here we have to wrap the function inside a round bracket, then at the end, we have to use a set of empty round brackets to make it a function expression.
Callback Function
A callback function is a special type of function used in Node.js, which is passed as an argument to a function call.
Summary
A function plays a useful role in Node.js, it helps write code by providing a way to combine several expressions into a group. It also gives a way to reduce the duplication of code by calling the group of expressions just by using a single-line function call. It helps to create a custom operation according to the need of the application.
There are several types of functions in Node.js, one of them is the arrow function which removes the use of annoying function keywords instead you just have to use an arrow symbol. We can also directly call a function when it is created by using the function expression which also helps in reducing the extra lines of code used to call a function.