Pure and Impure Functions in React/JS

Pure and Impure Functions in React/JS

Pure Functions

A Pure function is a function that does not modify any external variable. And the Impure function modifies the external variable. 

A basic principle of functional programming is that it avoids changing the application state and variables outside its scope. Let see how it works in React.

var s=10;

exampleOfPureFunc=(a,b)=>{

return a+b;

}

exampleOfPureFunc(10,5);

A pure function just takes some input and returns the output accordingly but it never changes any state of the component, it does not affect any external variables as well, like the value of s=10; so a pure function doesn't any effect on this global variable. 

  1. You can say that pure functions have no side effects.
  2. Pure functions must not rely on variables outside their scope
  3. Examples of pure functions are strlen(), pow(), sqrt(), array.length etc


Impure Functions

Impure Functions have side effects it does make changes on external variables like this.

exampleOfImpureFunc = (a, b) => {

   s = s + a + b;

   return s;

}

exampleOfImpureFunc(10, 5);

// one more example here 

// I am just creating a fake Object 


const user = {

   username: "Azeemaleem",

   password: "pakistan"

}

let isValidate = false

const validator = (user) => {

   if (user.username.length > 5 && user.password.length > 5) {

     isValidate = true;

   }

}

validator(user);

console.log(isValidate); //The value will be true.


You can see that it is causing a side effect(changes) on the external variable. Examples of impure functions are printf(), rand(), time(), etc.

Ellis Otoo

Senior Javascript Developer | LinkedIn Assessed: Top 5% of 2.6M people (Javascript)

1 年

Awesome Post! I think you should add that although you're passing the user object an argument which makes it look likes it's being reassigned to the user parameter, any modification to then user object in the validators function's scope also makes it impure, since objects are passed by reference. const validator?= (user) => { ???if (user.username.length > 5 && user.password.length > 5) { ?????isValidate = true; ???} } validator(user); ??

回复
Arv -

Frontend Developer(React) | JavaScript, Redux ,Strong DSA

2 年

Is it still pure function? if Function add(a,b) { return a= a+b; }

Afaque .

Frontend Developer | WordPress | ACF | CPTUI | Custom Themes Development | Website | UI/UX | WooCommerce

4 年

You should also post it on meduim(there update have made it slow but ) or dev.to

Malik Farhan

SR.Software Engineer(Tech Lead) at DevNatives

4 年

Factual (y)

Israr Ali

Gen AI | Senior Full-Stack Engineer | AI Team Lead | AI Agents

4 年

Love this

要查看或添加评论,请登录

Azeem Aleem的更多文章

  • Stop Using Inline Styles in React js

    Stop Using Inline Styles in React js

    Always avoid passing inline Styles to any react components. It's a little tiny thing that causes a huge problem in our…

    28 条评论
  • React + Leaflet in web Applications

    React + Leaflet in web Applications

    We use a lot of mobile apps which are using map services. Same like google map, Uber, Careem and Bykea mobile…

    2 条评论
  • Difference Between Git & Github

    Difference Between Git & Github

    Git Git is a version control system for tracking changes in computer files/Your local system. But what is the version…

    1 条评论
  • Interview-Based Concept (CONST)

    Interview-Based Concept (CONST)

    Concept of const is a little bit tricky which you should understand if you are a javascript developer. When anybody…

    2 条评论
  • How does JS Hoisting work?

    How does JS Hoisting work?

    Variable Hoisting is a by the default behaviour of javascript to compile the code in a specific manner. We mostly…

  • Can I use Multiple useEffects in a single Component?

    Can I use Multiple useEffects in a single Component?

    Using the useEffect hook we can inform the react that we need some data after component render. It can handle any side…

    4 条评论
  • REST API vs Web API

    REST API vs Web API

    What is the difference between API and REST API? Not all APIs are REST, but all REST services are APIs So let's start a…

    4 条评论
  • What is Async/await in React

    What is Async/await in React

    "Await" only works inside the Async functions. As you know that asynchronous function tasks are never dependent on each…

    7 条评论

社区洞察

其他会员也浏览了