Skip to content

Commit 1966515

Browse files
feat: add functions_deeply.js file
1 parent 85eca22 commit 1966515

File tree

2 files changed

+71
-25
lines changed

2 files changed

+71
-25
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Function Statement (a.k.a. Function Declaration)
2+
a(); // Works because of hoisting
3+
function a() {
4+
console.log("a called");
5+
}
6+
7+
// Function Expression
8+
// Not hoisted like declarations
9+
let b = function () {
10+
console.log("b called");
11+
};
12+
b();
13+
14+
// Anonymous Function (function without a name)
15+
// Usually used in function expressions or callbacks
16+
let c = function () {
17+
console.log("anonymous function");
18+
};
19+
c();
20+
21+
// ❌ Not valid alone:
22+
// function () { console.log("error"); }; // SyntaxError
23+
24+
// Named Function Expression
25+
let d = function abcd() {
26+
console.log("Named function expression");
27+
};
28+
d();
29+
// console.log(abcd); // ❌ ReferenceError (name is not accessible outside)
30+
31+
// First-Class Functions
32+
// Functions can be passed as arguments, returned from other functions, and stored in variables
33+
34+
function sayHello() {
35+
return "Hello!";
36+
}
37+
38+
function executeFn(fn) {
39+
console.log("Result:", fn()); // Passing function as argument
40+
}
41+
executeFn(sayHello);
42+
43+
// Returning a function
44+
function outer() {
45+
return function () {
46+
console.log("I’m inside a returned function");
47+
};
48+
}
49+
const innerFn = outer();
50+
innerFn(); // Outputs: I’m inside a returned function
51+
52+
// Arrow Functions (ES6)
53+
// Shorter syntax, `this` is lexically scoped
54+
const greet = (name) => {
55+
console.log(`Hello, ${name}`);
56+
};
57+
greet("Rafay");
58+
59+
// IIFE – Immediately Invoked Function Expression
60+
(function () {
61+
console.log("IIFE called immediately!");
62+
})();
63+
64+
// Callback Functions
65+
function loadData(callback) {
66+
console.log("Fetching data...");
67+
callback(); // calling the passed function
68+
}
69+
loadData(function () {
70+
console.log("Callback called after fetching.");
71+
});

part3 (Fundamental Concepts)/05_functions_deeply.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)