---
title: "What is Pure functions and Impure functions in javascript?"  
description: "What is Pure functions and Impure functions in javascript?"  
author: "Rahul Roi"  
published: 2021-04-03  
updated: 2021-04-03  
canonical: https://www.mindstick.com/forum/156433/what-is-pure-functions-and-impure-functions-in-javascript  
category: "javascript"  
tags: ["javascript"]  
reading_time: 2 minutes  

---

# What is Pure functions and Impure functions in javascript?

What is Pure [functions](https://www.mindstick.com/forum/160140/explain-the-role-of-functions-as-a-service-faas-in-serverless-computing) and Impure functions in [javascript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript)?

## Replies

### Reply by Shrikant Mishra

#### Pure functions Vs. Impure functions

#### Pure functions

The pure functions always return the same result when the same arguments are passed in. And, this function does not underlet on any state, or data, change during a program's execution. That is must only depend on its input arguments. So, This function does not have any side-effects like network or database calls and does not modify the arguments which are passed to them.

## example

```
function getSquare(x) {
   return x * x;
}
```

#### Impure functions

Whereas the Impurefunctions can change the internal state of one of its arguments or the value of some external variable. It can have any side-effects such as networks or database calls and it can modify the arguments that have been passed to them.

## example

```
function getSquare(items) {
  var len = items.length;
  for (var i = 0; i < len; i++) {
    items[i] = items[i] * items[i];
  }
  return items;
}
```

**Math.random()** is an impure function; it can changes the internal state of the Math object so you get different values on successive calls.


---

Original Source: https://www.mindstick.com/forum/156433/what-is-pure-functions-and-impure-functions-in-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
