Skip to content

Commit 911a3e9

Browse files
callback functions
1 parent 6fce392 commit 911a3e9

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

functions/functions.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,50 @@ const arrowFunctionExample = () => {
7777
console.log("This is an arrow function.");
7878
};
7979

80-
arrowFunctionExample(); // Calling the arrow function
80+
arrowFunctionExample(); // Calling the arrow function
81+
82+
//callback functions
83+
// Callback functions are functions that are passed as arguments to other functions and are executed after a certain event or condition is met.
84+
// They are commonly used in asynchronous programming, event handling, and functional programming patterns.
85+
// Example of a callback function
86+
function callbackExample(callback) {
87+
console.log("Executing callback function...");
88+
callback(); // Calling the passed callback function
89+
}
90+
callbackExample(() => {
91+
console.log("This is the callback function being executed.");
92+
});
93+
94+
function xx(yy){
95+
console.log("This is a function that accepts a callback yy and called in xx.");
96+
yy(); // Calling the callback function passed to xx
97+
}
98+
99+
xx(function yy(){
100+
console.log("This is a callback function yy passed to xx.");
101+
});
102+
103+
let counter = 0;
104+
105+
// using a global variable to keep track of clicks but not recommended in production code
106+
// Using a global variable to keep track of clicks is not recommended in production code,
107+
// as it can lead to unexpected behavior and make the code harder to maintain.
108+
// Instead, consider using closures or state management libraries for better control over state.
109+
document.getElementById("clickMe")
110+
.addEventListener("click", function xyz() {
111+
console.log("Button clicked! ", ++counter); // This is a callback function executed on button click.
112+
});
113+
114+
function attachEventListener() {
115+
let localCounter = 0; // Using a local variable to keep track of clicks
116+
document.getElementById("clickMe2")
117+
.addEventListener("click", function closureExample() {
118+
console.log("Button clicked! ", ++localCounter); // This is a closure that captures the local variable.
119+
});
120+
}
121+
attachEventListener();
122+
123+
124+
125+
126+

functions/functions.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ arrowFunctionExample();
121121
- Cannot be used as constructors
122122

123123
---
124+
## 8. Callback Functions
125+
A callback function is a function passed as an argument to another function.
126+
They are commonly used in asynchronous programming, event handling, and functional programming patterns.
127+
Example of a callback function
128+
129+
```
130+
function callbackExample(callback) {
131+
console.log("Executing callback function...");
132+
callback(); // Calling the passed callback function
133+
}
134+
callbackExample(() => {
135+
console.log("This is the callback function being executed.");
136+
});
137+
138+
```
124139

125140
## Summary Table
126141
| Concept | Hoisted | Named | Can be Anonymous | Can be Value | Example Use Case |
@@ -133,4 +148,3 @@ arrowFunctionExample();
133148

134149
---
135150

136-
**Practice:** Try modifying the examples and see how hoisting, naming, and context affect function behavior in JavaScript.

functions/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<title>Functions in JavaScript</title>
77
<h1>Functions in JavaScript</h1>
8+
<button id="clickMe">Click Me Global variable count</button>
9+
<button id="clickMe2">Click Me Local variable count closure</button>
810
<script src="functions.js"></script>
911
</head>
1012
</html>

0 commit comments

Comments
 (0)