Skip to content

Commit f0a01c5

Browse files
feat: add four ways to write functions
1 parent b61b4af commit f0a01c5

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// INFO: Four ways to write function
2+
3+
/*
4+
NOTE: Why this matters ?
5+
JavaScrpit allows multiple ways to define fucntions, and each has different behaviour in terms of:
6+
1. Hoisting
7+
2. this binding
8+
3. Syntax clarity
9+
4. Use cases
10+
*/
11+
12+
// 1. Function Declaration
13+
greet();
14+
function greet() {
15+
console.log("Hello");
16+
}
17+
18+
// 2. Function Expression
19+
const user = function(user) {
20+
console.log(`Welcome ${user}`);
21+
}
22+
user("Rafay");
23+
24+
// 3. Arrow Function (ES6)
25+
const add = (a, b) => a + b;
26+
add(3, 3);
27+
28+
29+
// 4. IIFE
30+
(function() {
31+
console.log("I run immediately!");
32+
})();

0 commit comments

Comments
 (0)