Skip to main content

Command Palette

Search for a command to run...

Impure function VS side Effect

Updated
1 min readView as Markdown
Impure function VS side Effect

Pure functions are predictable and they will always return a value.

Impure functions are not predictable, they may or may not return a value, and even if they return a value it may be different even if same paramtered are passed

Pure functions do not have side effects. Impure functions can cause side effects. Pure functions return the same output if we use the same input parameters. However, impure functions give different outcomes when we pass the same arguments multiple times.

https://www.syncfusion.com/blogs/post/pure-and-impure-functions-in-javascript-a-complete-guide.aspx#:~:text=Pure%20functions%20do%20not%20have%20side%20effects.,the%20same%20arguments%20multiple%20times.

Pure functions are functions that always return the same value for same inputs, for example:

function add(a,b) { 
  return a + b
}
console.log(add(4,5));

on the other hand an impure function might be something which won't always return the same value for same input example :

Math.random() it will always return a different value

A side effect is any function performs which is not related to predicting final output , like making a http request, changing something on the dom...

More from this blog

Omar's blog

135 posts