{"id":3946,"date":"2022-08-31T21:44:57","date_gmt":"2022-09-01T02:44:57","guid":{"rendered":"https:\/\/www.becomebetterprogrammer.com\/?p=3946"},"modified":"2022-08-31T21:51:29","modified_gmt":"2022-09-01T02:51:29","slug":"javascript-pure-and-impure-functions","status":"publish","type":"post","link":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/javascript-pure-and-impure-functions\/","title":{"rendered":"A Complete Guide to Pure and Impure Functions in JavaScript"},"content":{"rendered":"\n<p>There are two popular terms you will encounter when working with functional programming in Javascript &#8211; Pure and Impure functions. It&#8217;s also a common topic you will likely come across in most Javascript interviews. On the surface level, the two might look quite similar. However, it would be best to have an in-depth understanding as a developer to get the most out of it.<\/p>\n\n\n\n<p><strong>An impure function is a function with one or more side effects. A pure function is a function without any side effects.<\/strong><\/p>\n\n\n\n<p>That definition might sound a bit too complex, especially if you don&#8217;t have a good understanding of &#8220;side effects&#8221; in programming. However, don&#8217;t panic! This post will give you a detailed guide on pure and impure functions, Side effects, why Side effects are necessary, and what you should watch out for when dealing with side effects.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\"><p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<\/div><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/javascript-pure-and-impure-functions\/#What_are_Side_Effects\" >What are Side Effects?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/javascript-pure-and-impure-functions\/#Example_1_Add_a_New_Value_to_an_Old_Value\" >Example 1: Add a New Value to an Old Value<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/javascript-pure-and-impure-functions\/#Side_Effect_1_Dependency_on_an_External_Value\" >Side Effect 1: Dependency on an External Value<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/javascript-pure-and-impure-functions\/#Side_Effect_2_Modifies_a_Code_Block_Outside_its_Scope\" >Side Effect 2: Modifies a Code Block Outside its Scope<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/javascript-pure-and-impure-functions\/#Side_Effect_3_The_Function_is_Unpredictable\" >Side Effect 3: The Function is Unpredictable<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/javascript-pure-and-impure-functions\/#Example_2_Print_Text_to_Console\" >Example 2: Print Text to Console<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/javascript-pure-and-impure-functions\/#How_Does_the_consolelog_Cause_a_Side_Effect\" >How Does the console.log() Cause a Side Effect?<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/javascript-pure-and-impure-functions\/#Other_Examples_of_Side_Effects\" >Other Examples of Side Effects<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/javascript-pure-and-impure-functions\/#What_is_a_Pure_Function\" >What is a Pure Function?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/javascript-pure-and-impure-functions\/#Advantages_of_Pure_Functions\" >Advantages of Pure Functions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/javascript-pure-and-impure-functions\/#Things_to_Note_When_Working_With_Pure_Functions\" >Things to Note When Working With Pure Functions<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/javascript-pure-and-impure-functions\/#Pure_Functions_allow_Cloning_of_an_External_State\" >Pure Functions allow Cloning of an External State.<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/javascript-pure-and-impure-functions\/#Avoid_Code_Mutations\" >Avoid Code Mutations<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/javascript-pure-and-impure-functions\/#What_is_an_Impure_Function\" >What is an Impure Function?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/javascript-pure-and-impure-functions\/#Pure_Methods_in_Javascript\" >Pure Methods in Javascript<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/javascript-pure-and-impure-functions\/#Impure_Methods_in_Javascript\" >Impure Methods in Javascript<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/javascript-pure-and-impure-functions\/#Comparison_Between_Pure_and_Impure_Functions\" >Comparison Between Pure and Impure Functions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/javascript-pure-and-impure-functions\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_are_Side_Effects\"><\/span>What are Side Effects?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In functional programming, side effects occur when a function relies on or modifies an external code block outside its parameters to perform its operations. In other words, side effects occur when a function modifies a state outside its local scope. <\/p>\n\n\n\n<p>Below are several examples that will help you have a solid understanding of side effects. This information will help you differentiate between a Pure and an Impure function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Example_1_Add_a_New_Value_to_an_Old_Value\"><\/span>Example 1: Add a New Value to an Old Value<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Have a look at the code below.<\/p>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>var score = 0;\n\nfunction addScore(newScore) { \n  return score += newScore; \n}<\/code><\/pre>\n\n\n\n<p class=\"language-javascript\">It is a simple code snippet that seems to be calculating scores. You need to take note of two things here: the <code>var score = 0<\/code> variable and the <code>addScore()<\/code> function. Whenever the <code>addNums()<\/code> function is invoked, say <code>addScore(5)<\/code>, it will need to read the value of the <code>score<\/code> variable, add the parameter passed to it (in this case, it&#8217;s 5), and return the new value of the <code>score<\/code> variable.<\/p>\n\n\n\n<p>So what are the side effects present in this code?<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Side_Effect_1_Dependency_on_an_External_Value\"><\/span>Side Effect 1: Dependency on an External Value<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"language-javascript\">In the code above, the <code>addScore()<\/code> function depends on the score value. If, in any case, the <code>score<\/code> variable is omitted or is &#8220;undefined,&#8221; the <code>addScore()<\/code> function will always return an error.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Side_Effect_2_Modifies_a_Code_Block_Outside_its_Scope\"><\/span>Side Effect 2: Modifies a Code Block Outside its Scope<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"language-javascript\">When defining side effects above, this post said, &#8220;<em>side effects occur when a function modifies a state outside its local scope<\/em>.&#8221; In this scenario, the <code>addScore()<\/code> function is modifying the state of the <code>score<\/code> variable that is outside its local scope.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Side_Effect_3_The_Function_is_Unpredictable\"><\/span>Side Effect 3: The Function is Unpredictable<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"language-javascript\">When you look at the <code>addScore()<\/code> function alone, you can easily understand what it&#8217;s doing but cannot be sure of the output. It&#8217;s a non-deterministic function. That&#8217;s because there are external factors that determine its final result. In this case, the value of the <code>score<\/code> variable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Example_2_Print_Text_to_Console\"><\/span>Example 2: Print Text to Console<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Have a look at the code below.<\/p>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>function studentName() {\n  console.log(\"My name is John Doe\");\n}<\/code><\/pre>\n\n\n\n<p class=\"language-javascript\">That is a simple function that prints the hard-coded name of the student. However, there is still a side effect to this function. The <code>console.log()<\/code> function. But how?<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_Does_the_consolelog_Cause_a_Side_Effect\"><\/span>How Does the console.log() Cause a Side Effect?<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"language-javascript\">The presence of a <code>console.log()<\/code> in a function changes the state of the application. Although you are not modifying any external data here, you are changing what is rendered on the screen. In short, you are modifying the current state of the application. <\/p>\n\n\n\n<p class=\"language-javascript\">The <code>console.log()<\/code> does two things that make a function impure:<\/p>\n\n\n\n<ul class=\"language-javascript wp-block-list\"><li>The function depends on the <code>console.log()<\/code> object to achieve its purpose.<\/li><li>The function is modifying the state of an external code block. In this case, it&#8217;s the <code>console.log()<\/code> object.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Other_Examples_of_Side_Effects\"><\/span>Other Examples of Side Effects<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Other function operations considered as side effects include:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>DOM manipulation. Modifying, removing, or adding elements to a website using Javascript is referred to as a side effect because it changes the website&#8217;s current state.<\/li><li>Making HTTP requests.<\/li><li>Modifying the value of a global variable.<\/li><li>Reading and Writing files.<\/li><li>Modifying the value of an argument.<\/li><\/ul>\n\n\n\n<p>Up to this point, you have learned what is a side effect in functional programming. With that information, you can now dive into Pure and Impure functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_a_Pure_Function\"><\/span>What is a Pure Function?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Pure functions are functions that do not have any side effects. They can also be termed as functions without an internal state. Therefore, any operation carried out by these functions is not dependent on the state. They are predictable functions; if given the same input, you can predict the result regardless of how many times you invoke the function.<\/p>\n\n\n\n<p>Look at the example below.<\/p>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>function addScore(x,y) { \n  return x + y\n}\nconsole.log(`The Score is: ${addScore(12,15)}`)<\/code><\/pre>\n\n\n\n<p class=\"language-javascript\">A simple <code>addScore()<\/code> function calculates the score depending on the parameters passed. It does not depend on or modify any external code block. It is a predictable function. All these features make <code>addScore()<\/code> a pure function.<\/p>\n\n\n\n<p>Always use pure functions in your code wherever possible. They don&#8217;t have a state and can share multiple instances in a class. Below are several advantages of using pure functions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Advantages_of_Pure_Functions\"><\/span>Advantages of Pure Functions<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>They are independent functions<\/strong>: Pure functions do not modify or depend on the state of an external code block. All the input used to perform operations is passed as parameters. There are no external factors that affect the operation of this function. It is predictable.<\/li><li><strong>Easy to debug and read<\/strong>: Since pure functions don&#8217;t depend on any external code block or state, they are easy to debug and read. You only have to look within the function body, not any other code pieces.<\/li><li><strong>Reusable<\/strong>: Pure functions can be easily reused in different sections of your code without altering its contents or the caller.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Things_to_Note_When_Working_With_Pure_Functions\"><\/span>Things to Note When Working With Pure Functions<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Now that you have a good understanding of pure functions in Javascript, there are a few things that you need to keep in mind.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Pure_Functions_allow_Cloning_of_an_External_State\"><\/span>Pure Functions allow Cloning of an External State.<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>In the definition, this post described pure functions as functions that do not modify any external state. Therefore, cloning\/ copying an external state to a pure function doesn&#8217;t make it impure, as you only perform a copy-paste operation.<\/p>\n\n\n\n<p>Take a look at the code below.<\/p>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>const score = &#91;12,13];\n\nfunction updateScore(newScore, array) {\n  const clonedScore = &#91;...array];\n  clonedScore&#91;clonedScore.length] = newScore;\n  return clonedScore;\n}\n\nconsole.log(updateScore(17, score));<\/code><\/pre>\n\n\n\n<p class=\"language-javascript\">Here, the <code>updateScore()<\/code> function uses the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/Spread_syntax\" target=\"_blank\" rel=\"noopener\">spread operator<\/a> to copy the contents of the score array into <code>clonedScore<\/code> array declared inside the function body. <code>updateScore()<\/code> is a pure function as it does not modify or depend on the state of the score array. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Avoid_Code_Mutations\"><\/span>Avoid Code Mutations<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Although you can change the state of a variable defined locally in a pure function, it is not recommended. Take a look at the code below.<\/p>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>const score = &#91;12,13];\n\nfunction updateScore(newScore, array) {\n  const clonedScore = &#91;...array];\n  clonedScore&#91;clonedScore.length] = newScore;\n  return clonedScore;\n}\n\nconsole.log(updateScore(17, score));<\/code><\/pre>\n\n\n\n<p class=\"language-javascript\">The <code>updateScore()<\/code> function above is using <code>clonedScore[clonedScore.length] = newScore<\/code> to alter its state. Although that operation does not make <code>updateScore()<\/code> an impure function, it&#8217;s not the best practice.<\/p>\n\n\n\n<p>The recommended way is to pass all the values as function parameters, as shown in the code below.<\/p>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>const score = &#91;12,13];\n\nfunction updateScore(newScore, array) {\n  return &#91;...array, newScore];\n}\n\nconsole.log(updateScore(17, score));<\/code><\/pre>\n\n\n\n<p>Simple and clear! The above code does the same thing as the previous one, but it&#8217;s now much simpler and easy to understand.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_an_Impure_Function\"><\/span>What is an Impure Function?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>An impure function is unpredictable and contains one or more side effects. It&#8217;s a function that depends on or modifies data outside its lexical scope.<\/p>\n\n\n\n<p>Take a look at the example below.<\/p>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>var score = 0;\n\nfunction addScore(a,b){ \n  score = 5; \n  return a + b + score\n}\n\nconsole.log(addScore(10,15))<\/code><\/pre>\n\n\n\n<p class=\"language-javascript\">The <code>score<\/code> variable is declared outside the <code>addScore()<\/code> function scope and initially set to zero. However, its state is changed inside the <code>addScore()<\/code> function in the line <code>score = 5<\/code>. Automatically, the <code>addScore()<\/code> function becomes an impure function because it&#8217;s modifying the state of a code block outside its scope, making it have some side effects.<\/p>\n\n\n\n<p>From the description, you can deduce that two main properties make a function impure.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>It is unpredictable<\/li><li>It causes some side effects<\/li><\/ul>\n\n\n\n<p>Let&#8217;s look at one more example of an impure function that looks similar to a pure function. Take a look at the code below.<\/p>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>\/\/IMPURE FUNCTION\n\nconst myArray = (arrOne, val) =&gt; {\n  arrOne.push(val);\n  return arrOne;\n};\n\nconsole.log(myArray(&#91;1, 2, 3], 4));\n\/\/Returns:  &#91;1,2,3,4]<\/code><\/pre>\n\n\n\n<p class=\"language-javascript\">At first glance, this function appears to have no side effects, and one can easily conclude it as a pure function. However, take an in-depth look at this line &#8211; <code>arrOne.push(val)<\/code>. Here, the <code>myArray()<\/code> function directly mutates the memory in place of the <code>arrOne<\/code> array. In short, it is modifying the original array passed to the function.<\/p>\n\n\n\n<p>To achieve the same result with a pure function, you can use the spread operator to make a copy of the original array. Any operations done on this newly copied array won&#8217;t affect the original. See the code below.<\/p>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>\/\/PURE FUNCTION\n\nconst myArray = (arrOne, val) =&gt; {\n  return &#91;...arrOne, val]\n};\n\nconsole.log(myArray(&#91;1, 2, 3], 4));\n\/\/Returns:  &#91;1,2,3,4]<\/code><\/pre>\n\n\n\n<p>What if you were working with objects? Take a look at the code below.<\/p>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>\/\/IMPURE FUNCTION\n\nconst myObject = (obj, key, val) =&gt; {\n  obj&#91;key] = val;\n  return obj;\n};\nconsole.log(myObject({name:\"John Doe\"},\"age\",36));\n\/\/Returns:  { name: 'John Doe', age: 36 }<\/code><\/pre>\n\n\n\n<p class=\"language-javascript\">The <code>myObject()<\/code> function above is considered impure because it&#8217;s mutating the passed object parameter. Check this line &#8211; <code>obj[key] = val<\/code>. <\/p>\n\n\n\n<p class=\"language-javascript\">To convert this to a pure function, you can use the spread operator, as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>\/\/PURE FUNCTION\n\nconst myObject = (obj, key, val) =&gt; {\n  return {...obj,&#91;key]:val};\n};\nconsole.log(myObject({name:\"John Doe\"},\"age\",36));\n\/\/Returns:  { name: 'John Doe', age: 36 }<\/code><\/pre>\n\n\n\n<p>In the above code, we copy the object&#8217;s contents and do not modify it directly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Pure_Methods_in_Javascript\"><\/span>Pure Methods in Javascript<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Below are methods that are inherently considered pure in Javascript.<\/p>\n\n\n\n<ul class=\"language-javascript wp-block-list\"><li>The <code>spread ...<\/code> operator used to create copies.<\/li><li><code>arr.map()<\/code><\/li><li><code>arr.filter()<\/code><\/li><li><code>arr.reduce()<\/code><\/li><li><code>arr.each()<\/code><\/li><li><code>arr.every()<\/code><\/li><li><code>arr.concat()<\/code><\/li><li><code>arr.slice()<\/code><\/li><li><code>Math.floor()<\/code><\/li><li><code>str.toLowerCase()<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Impure_Methods_in_Javascript\"><\/span>Impure Methods in Javascript<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Below are methods that are inherently considered impure in Javascript.<\/p>\n\n\n\n<ul class=\"language-javascript wp-block-list\"><li><code>Math.random()<\/code>: This is regarded as an impure function because it produces a different result on each invocation.<\/li><li><code>Date.now()<\/code><\/li><li><code>arr.splice()<\/code><\/li><li><code>arr.push()<\/code><\/li><li><code>arr.sort()<\/code><\/li><li>Since Javascript is synchronous, asynchronous Javascript methods like <code>fetch()<\/code> and <code>async()<\/code> are also considered impure functions.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Comparison_Between_Pure_and_Impure_Functions\"><\/span>Comparison Between Pure and Impure Functions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>So, which is which? Do you go with pure functions or impure functions?<\/p>\n\n\n\n<p>There is no single winner between the two as they all play different roles in a program. For example, making DOM manipulations or HTTP requests with a pure function would be impossible. On the other hand, using too many impure functions in your code might easily result in <a href=\"https:\/\/www.becomebetterprogrammer.com\/software-development-code-anti-patterns\/\">Spaghetti Code<\/a>, especially in large applications.<\/p>\n\n\n\n<p>Below is a simple comparison table that will make it easier to differentiate between the two functions.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Pure Functions<\/strong><\/td><td><strong>Impure Functions<\/strong><\/td><\/tr><tr><td>Have no side effects<\/td><td>Cause multiple side effects<\/td><\/tr><tr><td>They will always return the same result regardless of how many times the function is invoked.<\/td><td>They return a different result on every successive function call.<\/td><\/tr><tr><td>They are easy to read and debug<\/td><td>They are hard to read and debug since they are affected by external code blocks.<\/td><\/tr><tr><td>They come in handy in certain contexts<\/td><td>They come in handy in certain contexts<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This post has given you a detailed guide on pure and impure functions in Javascript. It has also looked at several examples and the advantages of using any of them. Pure functions are highly recommended as they are easy to read and debug. <\/p>\n\n\n\n<p>Unfortunately, you cannot rely on them fully since there are operations they cannot perform. For example, DOM manipulations and making HTTP requests. In cases where you need to use impure functions, take note of the side effects to avoid any arising issues in your code.<\/p>\n\n\n\n<p><strong>Was this article helpful? Is there any additional information that you feel was left out?<\/strong><\/p>\n\n\n\n<p>You can also share your thoughts by replying on Twitter of&nbsp;<a href=\"https:\/\/twitter.com\/bbprogrammer\/status\/1459150572565798916\" target=\"_blank\" rel=\"noreferrer noopener\">Become A Better Programmer<\/a>&nbsp;or to&nbsp;<a href=\"https:\/\/twitter.com\/NetmattaTech\" target=\"_blank\" rel=\"noreferrer noopener\">my personal account<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-embed aligncenter is-type-rich is-provider-twitter wp-block-embed-twitter\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"twitter-tweet\" data-width=\"550\" data-dnt=\"true\"><p lang=\"en\" dir=\"ltr\">Hey <a href=\"https:\/\/twitter.com\/hashtag\/javascript?src=hash&amp;ref_src=twsrc%5Etfw\" target=\"_blank\" rel=\"noopener\">#javascript<\/a> developer, Are your functions pure? <br><br>Do you know what pure and impure functions are?<br>pure = 0 side effects<br>impure =  +1 side effects<a href=\"https:\/\/twitter.com\/NetmattaTech?ref_src=twsrc%5Etfw\" target=\"_blank\" rel=\"noopener\">@NetmattaTech<\/a> explains these two concepts in his article. Check it out!<a href=\"https:\/\/t.co\/jhGlU6qC1R\">https:\/\/t.co\/jhGlU6qC1R<\/a><\/p>&mdash; Become A Better Programmer (@bbprogrammer) <a href=\"https:\/\/twitter.com\/bbprogrammer\/status\/1565170332721373184?ref_src=twsrc%5Etfw\" target=\"_blank\" rel=\"noopener\">September 1, 2022<\/a><\/blockquote><script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>There are two popular terms you will encounter when working with functional programming in Javascript &#8211; Pure and Impure functions. It&#8217;s also a common topic you will likely come across in most Javascript interviews. On the surface level, the two might look quite similar. However, it would be best to have an in-depth understanding as &#8230; <a title=\"A Complete Guide to Pure and Impure Functions in JavaScript\" class=\"read-more\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/javascript-pure-and-impure-functions\/\" aria-label=\"More on A Complete Guide to Pure and Impure Functions in JavaScript\">Read more<\/a><\/p>\n","protected":false},"author":5,"featured_media":4037,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3946","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/posts\/3946","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/comments?post=3946"}],"version-history":[{"count":5,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/posts\/3946\/revisions"}],"predecessor-version":[{"id":4039,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/posts\/3946\/revisions\/4039"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/media\/4037"}],"wp:attachment":[{"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/media?parent=3946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/categories?post=3946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/tags?post=3946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}