Javascript

Pure Functions in Javascript with example

Javascript Pure Functions

Hello coders, welcome back to another article on the ‘Javascript’ series. Today’s article is based on the topic “Pure Functions in Javascript with example”. After reading this article, you all have a very clear understanding of what is pure functions and what are its applications. so without wasting much time, let’s dive in.

What are Pure Functions in Javascript?

Pure Functions are those function who always returns the same result for the same argument values i.e same values passed to the function as arguments and it doesn’t modify the argument or output something else.

Also Read: What is Closure in Javascript?

How Do Pure Functions work in Javascript?

Pure functions accept input and return value without modifying any data outside its scope. Its output or return value must depend on the input/arguments and pure functions must return a value.

It must be noted that pure functions don’t care about time. Non-determinism and side effects add the notion of time. If a function depends on something that might or might not happen and changes something outside its walls, then it suddenly becomes dependent on time.

Although, Javascript is not a purely functional language; it treats functions as objects (first-class functions). We can also say that the pure function is always deterministic. This means, that given the same input, the function will always return the same output.

Examples of Pure Functions in Javascript

Here are some of the most common and simple examples that will help you to understand what pure functions is and how to use them in the best ways. So keep reading.

Demo of a simple pure function

 const add = (x, y) => x + y // A pure function 

Here we can see that add function is a pure function because its output is solely dependent on the arguments passed in it and will always produce the same output for the same given values. Let’s have a look at another example.

const magicLetter = 'I'
const createMagicPhrase = (phrase) => `${magicLetter}Keshav${phrase}`

we can easily see that the value of createMagicPhrase is depended on the external factor which is also external to its scope i.e magicLetter. Hence it is not a pure function.

Example of Pure Functions in Javascript

function greetUser(user, greeting = 'Hello') {
    return `${greeting}, ${user.firstName} ${user.lastName}!`
}
const user = {
    firstName: 'keshav',
    lastName: 'Dutt'
} 
// Expected Output. greetUser(joe) 
// 'Hello, keshav Dutt!' 

Benefits of using the pure function in Javascript.

  1. Pure functions are deterministic by nature, therefore writing unit tests for them is a lot simpler. Either your function works or it doesn’t work.
  2. As pure functions only depend on their input, and will not cause side effects, they are great for scenarios where parallel threads run and use shared memory.
  3. Pure functions are like little units of logic. They only depend on the input you feed them, you can easily reuse functions between different parts of your codebase or different projects altogether.

So, this is all about Javascript Pure Functions. Thanks for reading!

Leave a Comment