Pure Functions in Javascript
Javascript concepts in 10 points or less

Pure Functions in Javascript

Pure functions in Javascript are part of the functional programming paradigm. They apply to all programming languages using functions.

In Javascript, functions are first-class citizens. They can be passed to functions, returned from functions and assigned to variables. Here, let us see what are pure functions and why we need them.

1. By definition, a pure function is a function that always returns the same output for a given input.

2. A pure function is predictable, readable, reusable and testable.

3. It does not have any side effects. Side effects are the impact a function has on its outer environment or the inputs passed to it(mutation) or DOM manipulation etc. So a pure function does not affect its outer environment.

4. Mutating inputs(arrays) can be avoided by using the spread parameter and functions like map(), filter() and reduce(), which make shallow copies of the input array, thereby not changing the original array.

5. Pure functions should have at least one parameter and should return something.

6. There are many advantages of pure functions. They are easy to test, and debug, and result in clean code that is decoupled and independent and can be added to utility functions.

7. ?Functions that include API calls, database read and writes, logging, and filesystem manipulation are all impure functions, as they have side effects. However, it is not possible to do without these types of functions in application programming. Hence the idea is to have such functions to a minimum and have pure functions as much as possible as a best programming practice.

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

Kamakshi Mahadevan的更多文章

  • AbortController in React

    AbortController in React

    Today, let us discuss AbortControllers in React. 1.

  • Closures in Javascript

    Closures in Javascript

    Today we are going to discuss closures in Javascript. In one of our previous articles, https://www.

  • Javascript Object Notation (JSON)

    Javascript Object Notation (JSON)

    Today’s topic is JSON. Let’s look at what JSON is and what it is used for.

  • Higher Order Components in React

    Higher Order Components in React

    Let us look at Higher Order Components in React. Definition: A higher-order component in React is nothing but a…

  • Debouncing and throttling in Javascript

    Debouncing and throttling in Javascript

    Debouncing and throttling are two techniques in Javascript that optimize performance by preventing a function from…

  • React Lifecycle and useEffect

    React Lifecycle and useEffect

    Today let us look at the React lifecycle and its methods. We will also learn about useEffect and how it is affected by…

  • Higher Order Functions in Javascript

    Higher Order Functions in Javascript

    Continuing with the functional programming paradigm from last week, today let us look at Higher Order Functions in…

  • ReactJS Best Practices

    ReactJS Best Practices

    Today, we will discuss 10 ReactJS best practices. 1.

  • Javascript - Context, scope and hoisting

    Javascript - Context, scope and hoisting

    Let us delve into the concepts of context, scope and hoisting in Javascript in today’s article. 1.

    2 条评论
  • Javascript - this, call(), bind(), apply()

    Javascript - this, call(), bind(), apply()

    Understanding the this keyword is often a challenge for Javascript developers. Here, I have tried to explain how the…

社区洞察

其他会员也浏览了