42
loading...
This website collects cookies to deliver better user experience
1. There will be always the same output for the same input.
2. There will be no side effects.
const add = (a,b) => a + b;
add(1,2); // 3
a
and b
and it will give the same result for that argument which is always a + b
.let user = {
firstName: "Michael",
lastName: "Jackson",
gender: "M"
}
function getFullName(user) {
return `${user.firstName} ${user.lastName}`;
}
console.log(getFullName(user)); // Michael Jackson
getFullName
is a pure function, because getFullName
is not mutating the state.1. Create/update database.
2. http/s call.
3. Change the file system.
4. Mutate the state.
etc
Math.random();
Math.random()
is an impure function because it always returns a different output.console.log
is a pure function, it returns undefined for any input.console.log
is not a pure function because it has side effects, console.log
is using another share environment to log into the console.const add = function (a,b){
console.log("added");
return a + b;
}
add(1,2); // 3
console.log
is not effecting our output, then it is not a pure function. Because the add
function has a side effect.let user = {
firstName: "Michael",
lastName: "Jackson",
gender: "M"
}
function getFullName(user) {
user.firstName = user.gender === "M" ? `Mr. ${user.firstName}`: `Mrs. ${user.firstName}`;
return `${user.firstName} ${user.lastName}`;
}
console.log(getFullName(user)); // Mr. Michael Jackson
getFullName
is an impure function, because getFullName
is mutating the state. Inside the function definition we are assigning a value to the object property.let cart = {
items: [{
name: "X",
price: 10,
quantity: 1
}]
}
function addItem(cart){
let newCart = {...cart}
newCart.items.push({
name: "Y",
price: 5,
quantity: 2
});
return newCart;
}
console.log(cart); // {items: Array(1)}
let newCart = addItem(cart); // changing state
console.log(cart); // {items: Array(2)}
console.log(newCart); // {items: Array(2)}
let cart = {
items: [{
name: "X",
price: 10,
quantity: 1
}]
}
function deepClone(value){
return JSON.parse(JSON.stringify(value)); // for example purpose
}
function addItem(cart){
let newCart = deepClone(cart);
newCart.items.push({
name: "Y",
price: 5,
quantity: 2
});
return newCart;
}
console.log(cart); // {items: Array(1)}
let newCart = addItem(cart); // changing state
console.log(cart); // {items: Array(1)}
console.log(newCart); // {items: Array(2)}
Pure function can be easily predicted, it is convenient to test. Pure function makes state management easier.