All You Need to Know About Pure Functions & Impure Functions in JavaScript

Author: neptune | 02nd-Apr-2023 | Views: 84
#JavaScript #React.js

What is a function?

In JavaScript, a function is a block of code that performs a specific task. It is a reusable piece of code that can be called multiple times with different inputs to produce different outputs. Functions are an essential part of JavaScript programming, and they help in making the code more organised, modular, and easy to maintain.

Why JavaScript?

JavaScript is a versatile programming language that can be used for both front-end and back-end development. It has a vast collection of built-in functions that can be used to perform various tasks. However, not all functions are created equal. Some functions are pure, while others are impure.

What is a pure function?

A pure function is a function that always returns the same output for the same input. It does not have any side effects, which means it does not modify any external state or variables. Pure functions are predictable, and they do not depend on any external factors. They only depend on their input parameters.

For example, consider the following pure function that adds two numbers:


function add(a, b) {

    return a + b;



This function will always return the same output for the same input. If we call add(2, 3), it will always return 5. It does not modify any external state or variables.

What is an impure function?

An impure function is a function that has side effects. It modifies external state or variables, and it may not always return the same output for the same input. Impure functions are unpredictable, and they depend on external factors.

For example, consider the following impure function that modifies an external variable:

 let count = 0;

 function increment() {



This function modifies the external variable count, and it does not return any value. If we call increment() multiple times, the value of count will keep increasing. This function has side effects and is impure.

Why are pure functions useful?

Pure functions have several advantages over impure functions. They are predictable, which makes them easier to test and debug. They do not have any side effects, which makes them safer to use in a multi-threaded environment.

Pure functions are useful because they have several advantages over impure functions:

1. Predictability: Pure functions always return the same output for a given input. This makes them predictable and easier to reason about.

2. Testability: Since pure functions don't have any side effects, they are easier to test. You can test them by passing in different inputs and verifying that the output is correct.

3. Reusability: Pure functions can be reused in different parts of your codebase without worrying about side effects. This makes them more modular and easier to maintain.

4. Parallelism: Pure functions can be executed in parallel without any synchronisation issues. This can lead to better performance in certain scenarios.

5. Refactoring: Pure functions are easier to refactor because they don't have any dependencies on external state. You can change the implementation of a pure function without worrying about breaking other parts of your codebase.

Overall, pure functions promote good programming practices and can lead to more maintainable and scalable code.

How to create pure functions in JavaScript?

To create a pure function in JavaScript, you need to ensure that it does not modify any external state or variables. It should only depend on its input parameters and return a value. You should also avoid using any global variables or functions inside the pure function.

Here are some guidelines for creating pure functions in JavaScript:

1. Avoid modifying external state: A pure function should not modify any external state, such as global variables or objects passed by reference. Instead, it should only use its input arguments to compute the output.

2. Avoid using random values: A pure function should not use any random values or generate any random output. This can make the function unpredictable and difficult to test.

3. Avoid using current time or date: A pure function should not use the current time or date as input or output. This can make the function unpredictable and difficult to test.

4. Avoid using console.log: A pure function should not use console.log or any other side-effecting functions. This can make the function difficult to test and debug.

5. Use immutable data structures: A pure function should use immutable data structures, such as arrays and objects that cannot be modified. This ensures that the function does not accidentally modify any external state.

6. Return a value: A pure function should always return a value. This makes it easier to compose functions and reason about their behaviour.

Pure vs Impure Functions

Pure functions are better than impure functions in most cases. They are easier to test, debug, and reason about. They are also safer to use in a multi-threaded environment. However, impure functions are sometimes necessary, especially when dealing with external resources like databases or network calls.

Inbuilt pure functions in JavaScript:

JavaScript has several built-in pure functions that you can use in your code. Some of the most commonly used pure functions are:

- Math.abs()

- Math.max()

- Math.min()

- Array.concat()

- Array.slice()

- Array.join()

Inbuilt impure functions in JavaScript:

JavaScript also has several built-in impure functions that you should be aware of. Some of the most commonly used impure functions are:

- Array.push()

- Array.pop()

- Array.shift()

- Array.unshift()

- Array.splice()



In conclusion, pure functions are an essential part of JavaScript programming. They are predictable, safe, and easy to reason about. You should try to use pure functions whenever possible and avoid using impure functions unless necessary. By following this approach, you can write cleaner, more maintainable, and bug-free code.

Related Blogs
Create Your First App in React with Redux | Counter app
Author: neptune | 30th-Mar-2023 | Views: 378
Creating your first app in React can be a daunting task, but with the right guidance, it can be a fun and rewarding experience. Will guide you to create a counter app in React with redux...

React.js vs React Native – What's the Difference?
Author: neptune | 26th-Mar-2023 | Views: 336
React.js and React Native are both frameworks developed by Facebook for building user interfaces. However, they are not the same and have different use cases...

Arrow Functions in JavaScript | ES6
Author: neptune | 26th-Mar-2023 | Views: 305
#JavaScript #React.js
In this article, we will explore the syntax and usage of arrow functions in detail, along with some examples...

Essential Topics to Master React JS
Author: neptune | 31st-Mar-2023 | Views: 239
A Comprehensive Guide to Components, State, JSX, Event Handling, Routing, Redux, Hooks, Testing, Performance Optimization, and Server-Side Rendering...

Opportunities - React Django Developer
Author: neptune | 14th-Apr-2023 | Views: 165
#React.js #Django
React Django stack is popular for building web applications. Opportunities for React Django developers in Full Stack, Web, and Software development roles...

Functional vs Class Components in React: All you need to know?
Author: neptune | 21st-Apr-2023 | Views: 72
React components are building blocks for UIs. They can be functional (stateless) or class (stateful). Components are called using their names as HTML tags...

View More