Pure Functions in JavaScript Last Updated : 17 Dec, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report A Pure Function is a function (a block of code) that always returns the same result if the same arguments are passed. Pure functions return consistent results for identical inputs.They do not modify external states or depend on mutable data.Often used with immutable data structures to ensure reliability.Their independence from external states makes them highly reusable. JavaScript function add(a, b) { return a + b; } console.log(add(2, 3)); console.log(add(2, 3)); Output5 5 It always returns the same result for the same input.It does not modify any external variables or state.Note: Generally, we use the Pure function in JavaScript for any purpose. Characteristics of Pure FunctionsDeterministic Output: For a given set of inputs, the output is always the same.No Side Effects: The function does not:Modify variables outside its scope.Interact with external systems like APIs, databases, or DOM manipulation.Change mutable data.Immutability: Pure functions do not change the input values; instead, they return a new value or object.Example of a Function with Side EffectsHere, increment is not a pure function because it modifies the external variable count. JavaScript let c = 0; function inc() { c++; return c; } console.log(inc()); console.log(inc()); Output1 2 Impure Functions: What to AvoidImpure functions produce unpredictable results or affect external states, which can lead to bugs and make your code harder to test. JavaScript let user = { name: "Meeta", age: 25 }; function updated(newAge) { user.age = newAge; return user; } console.log(updated(26)); // Alters the global `user` object console.log(user.age); Output{ name: 'Meeta', age: 26 } 26 Real-World Applications of Pure FunctionsData Transformation: Use pure functions in map, filter, and reduce operations for processing data.State Management: Libraries like Redux emphasize pure functions for state updates.Unit Testing: Pure functions are ideal for unit tests because they have predictable outputs.Performance Optimization: Pure functions are easily memoized, as their outputs depend solely on inputs. Comment More infoAdvertise with us Next Article Pure Functions in JavaScript G geek5177 Follow Improve Article Tags : JavaScript Web Technologies javascript-functions Similar Reads Functions in JavaScript Functions in JavaScript are reusable blocks of code designed to perform specific tasks. They allow you to organize, reuse, and modularize code. It can take inputs, perform actions, and return outputs.JavaScriptfunction sum(x, y) { return x + y; } console.log(sum(6, 9)); // output: 15Function Syntax 5 min read JavaScript get Function JavaScript get function is used to access the properties of an object using dot notation or square brackets. It allows you to retrieve the value associated with a particular property key and the get function is often used when working with objects that implement JavaScript's getter function. The get 3 min read How to write a function in JavaScript ? JavaScript functions serve as reusable blocks of code that can be called from anywhere within your application. They eliminate the need to repeat the same code, promoting code reusability and modularity. By breaking down a large program into smaller, manageable functions, programmers can enhance cod 4 min read Abstraction in JavaScript In JavaScript, Abstraction can be defined as the concept of hiding the inner complex workings of an object and exposing only the essential features to the user. Hiding Complexity: Implementation is hidden, it shows only the necessary details.Modularity: Code is organized in a reusable form, which im 4 min read Why learn JavaScript Foundations ? JavaScript is like a cornerstone in the world of programming languages. It's famous for being adaptable and everywhere. No matter if you're into making websites, or mobile apps, or diving into server stuff, understanding the basics of JavaScript is super important. Let's talk about why it's so cruci 4 min read Like