Skip to content

Commit 596b893

Browse files
committed
learn about Pure and Impure functions
1 parent e99e7ed commit 596b893

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Advanced Function Concepts
2+
3+
| Contents |
4+
| :--- |
5+
| [Pure Functions and Side-Effect](#pure-functions-and-its-side-effect) |
6+
7+
## [Pure Functions and Side-Effects](https://drive.google.com/uc?export=view&id=1J1QRcr3UMC-h4zeQhgv6INAfitnKWa2_)
8+
9+
In JavaScript, a pure function is a function that:
10+
11+
- Given the same inputs, always returns the same output.
12+
- Has no side effects, meaning it doesn't modify anything outside of its own scope, including global variables, DOM elements, or any other external state.
13+
14+
Here's an example of a pure function:
15+
16+
```javascript
17+
function sum(a, b) {
18+
return a + b;
19+
}
20+
```
21+
22+
The `sum` function takes two arguments and always returns their sum. It doesn't have any side effects because it doesn't modify anything outside of its scope.
23+
24+
On the other hand, an impure function is a function that:
25+
26+
- Doesn't always return the same output for the same inputs.
27+
- Has side effects, meaning it modifies something outside of its own scope.
28+
29+
Here's an example of an impure function:
30+
31+
```javascript
32+
let counter = 0;
33+
34+
function increment() {
35+
counter++;
36+
return counter;
37+
}
38+
```
39+
40+
The `increment` function modifies the global counter variable every time it is called, which is a side effect. Additionally, it doesn't always return the same output for the same input, since it returns a different value each time it is called.
41+
42+
Side effects can be problematic in code because they can make it harder to reason about what a function does and can introduce bugs. It's generally a good practice to write as many pure functions as possible and minimize the number of impure functions.
43+
44+
Readings:
45+
46+
- [Pure and Impure Functions in JavaScript: A Complete Guide](https://www.syncfusion.com/blogs/post/pure-and-impure-functions-in-javascript-a-complete-guide.aspx)
47+
48+
- [Pure vs Impure Functions in Functional Programming – What's the Difference?](https://www.freecodecamp.org/news/pure-function-vs-impure-function/)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* Pure Function */
2+
3+
function add(num1, num2) {
4+
return num1 + num2;
5+
}
6+
7+
console.log(add(4, 5));
8+
console.log(add(19, 3));
9+
10+
/* Impure Function */
11+
12+
// On every reload, the returned value of these function will change
13+
function addRandom(num) {
14+
return num + Math.random();
15+
}
16+
17+
console.log(addRandom(7));
18+
console.log(addRandom(17));
19+
20+
// A function is also considered as impure if it introduces side effect, which means changes anything outside of function
21+
let previousResult = 0;
22+
function addMoreNumbers(num1, num2){
23+
const sum = num1 + num2;
24+
previousResult = sum; // Side-Effect
25+
return sum;
26+
}
27+
28+
console.log(addMoreNumbers(34, 21), previousResult);
29+
30+
const hobbies = ['Sports', 'Cooking'];
31+
32+
function printHobbies(hobbies) {
33+
console.log(hobbies);
34+
hobbies.push('Gardening');
35+
}
36+
37+
printHobbies(hobbies);
38+
console.log(hobbies);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Advanced Functions</title>
8+
<script src="app.js" defer></script>
9+
</head>
10+
<body>
11+
12+
</body>
13+
</html>

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- [Constructor Functions and Prototypes](Constructor-Functions-and-Prototypes/README.md)
1414
- [Advanced DOM APIs](Advanced-DOM-APIs/README.md)
1515
- [Working with Events](Working-with-Events/README.md)
16+
- [Advanced Function Concepts](Advanced-Function-Concepts/README.md)
1617

1718
## References
1819

0 commit comments

Comments
 (0)