So this article will be about what is functions in JavaScript and, most importantly, what is pure and impure function in JavaScript and how to differentiate between pure and impure functions.

Table of Contents

  1. What are functions
  2. What are Pure Functions
  3. What are Impure Functions
  4. Conclusion

What are Functions in JavaScript?

So before we know what are functions in JavaScript, we all know that JavaScript is a scripting language which means we can write our code line by line and save it as a script file name dot JS and JavaScript will execute that script file. Now the question is, why do we need a function?

So once the script file becomes very big, it becomes tough to identify and debug what a particular line of code is doing, and this is where JavaScript functions come into the picture; functions are the beauty of JavaScript. It helps to wrap a particular piece of script and helps to identify what a particular set of script is doing or what it is required to do.

Now let us understand functions with a few examples:

function sayHello() {
console.log(‘Hello’);
} 
sayHello(); // Hello

In the above example, we have created a function named sayHello(), and once we execute this function, it will print hello in our console. So now, only by seeing the function we can know or identify what a particular set of function is doing.

Now let's take another example to understand function more clearly,

function fullName(firstname, lastname) {
console.log(`${firstname} ${lastname}`);
}
fullName(‘Amit’, ‘Singh’) // Amit Singh

So in the above example, we have declared a function named fullName(), which takes two arguments: the first name and the last name, and prints the full name by taking the first name and the last name as arguments.

Now just by seeing the function code, we can identify what the particular set of function is doing or what it is required to do. It becomes very easy to debug a particular piece of code, which is why we use pure functions in javascript.

Now depending upon our need or requirement, we can create a set of functions in JavaScript to make our work easier and to debug a particular piece of code. For example, we can create a function to add a number, multiply a number, etc.

So as we have understood what functions are, will help us know better about pure and impure functions in JavaScript.

What is Pure function?

So basically, pure functions are a type of function in JavaScript that is pure in nature. By this, I mean this function will always produce the same output for the same given input, and there will be no side effects. These pure functions are independent and do not depend on any external state or variables.

Now let us understand pure functions with few examples:

function fullName(firstname, lastname) {
console.log(`${firstname} ${lastname}`);
}
fullName(‘Amit’, ‘Singh’) // Amit Singh

We have taken the same example we discussed above; we have declared a function named fullName(), which takes a first name and last name as arguments and gives the output, which is the full name.

So we can see that the output was very predictable for this function, which is why we call this kind of function a pure function where the output of a particular function is predictable, only depends on the internal state, and does not have any side effect. Pure functions can be reused in our code wherever we require the same output; this makes the program very modular.

Here are the key characteristics of pure functions:

Now by the above example, we can see that pure function has several advantages.

What is Impure Function?

So impure functions are functions that are impure in nature. I mean that given the same input, it will give different output. We can see that impure functions are the opposite of what we discussed above in pure functions.

So unlike pure functions, an impure function can depend on any external state and can also modify the external state, mutate data, perform input-output operation, or have other observable effects on the program state or the external state. Impure functions can be used in scenarios where we need to interact with external states. For example, let's check if a particular user is authenticated, and based on that, we will do a certain set of actions.

isUserAuthenticated() is a function that returns true or false based on the user's situation if the user is logged in or not.

function isUserLoggedIn() {
const isAuthenticated = isUserAuthenticated() // return true or false;
if(isAuthenticated) {
console.log(`user is logged in`)
} else {
console.log(‘user is not logged in’);
}
}

Now in the above example, we see that we have a function named is isUserAuthenticated(), which returns true or false based on the user situation if the user is logged in or not, and then we have declared a function named isUserLoggedIn() which depends on the external state that is a function isUserAuthenticated() and check if a user is authenticated or not and based on the state it gives the output.

So now, by seeing the function isUserLoggedIn(), we cannot predict the output because the output is based on the external state. Whether the user is authenticated or not, it will give the output.

Let us understand this by a simple example:

let c = 2
function add(a,b) {
console.log(a+b+c)
}
add(1,2); 

In the above example, we have declared a function named add(). It takes two arguments, a and b, and returns the sum of a + b + c; we can clearly see that function add() depends on the external state, i.e., variable c for the output, and the output will never be the same for the given input.

Now let us understand by another example:

function generateUserId(){
console.log(Math.floor(Math.random()*10000));
}

In the above example, we have created a function named generateUserId(), which basically generates a random user id using that external state; in this case, that is Math.random() functions. We can see that it becomes very difficult to predict the output of the generateUserId() function.

Here are some characteristics of impure functions:

Conclusion

In this article, we have covered what is functional programming, what are functions in JavaScript, why do we use functions in JavaScript, what are pure and impure functions in JavaScript, and what the differences are between impure and pure functions. So we can conclude that every function has its importance, whether pure or impure, depending on the situation or requirement. We can use pure or impure functions to make our code clean and can be easy to debug.