-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallbacks&forEach.js
More file actions
31 lines (21 loc) · 845 Bytes
/
callbacks&forEach.js
File metadata and controls
31 lines (21 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// call backs are function into a function kind of nested function for example
people = ['rohan' , 'harry' , 'swedish' , 'kinda'];
const logPerson = (person , index)=>{
console.log(`Good morning ${person} - ${index}`);
}
people.forEach(logPerson); // This is a callback function we can also write it as
console.log('\n Another way of writing is :')
// for each element of people it is gonna fire the function written inside it
people.forEach((person , index)=>{
console.log(`Good morning ${person} - ${index}`);
});
// simple : calling a function inside a function is call back function
/*
Task : log each person name into the browser
*/
const grabElement = document.getElementById('test');
let generatehtml = ``;
people.forEach((person)=>{
generatehtml += `<p>${person}</p>`;
});
grabElement.innerHTML = generatehtml;