Pure Functions in JavaScript Last Updated : 17 Dec, 2024 Comments Improve Suggest changes 32 Likes 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. Create Quiz Pure Functions Visit Course Comment G geek5177 Follow 32 Improve G geek5177 Follow 32 Improve Article Tags : JavaScript Web Technologies javascript-functions Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like