What is Pure Functions Javascript
In this article, we will see what is pure functions javascript.
Javascript Function
A function is a block of code, meant to perform a specific task.
Whenever we want to perform some specific task, we create a function.
Why function? function is created
- To divide a larger problem into smaller tasks or modules(manageability).
- For reusability(i.e. once a function is defined, it can be used multiple times).
- For abstraction(i.e hiding the way how tasks are done) (creating libraries).
Types of function
There are many ways in which we can create a function in javascript:
- Function declaration
- Function expression
- Function constructor
- IIFE ( Immediately Invokable Function Expression)
How to create a function using Function Declaration
A function defined with function declaration must begin with the function keyword.
Syntax:
function functionname([param1, param2, ...paramN]) // function header
{
statements;
}
Example:
A function without parameters and returning value
function display()
{
document.write("Hi");
}
display();
//output: Hi
Note: Every function gets executed only when we call them.
The syntax for calling the function:
functionname([param1, param2...paramN]);
Example : display();
If we call the function many times, the function is executed many times.