1/1/1970
Looping through Arrays
Arrays can be looped through using the classical javascript for loop or through some other methods discussed below
forEach loop -> Calls a function, once for each elementconst arr = [1, 2, 3]
arr.forEach((value, index, array)=>{
//function logic
})map() -> Creates a new array by performing some operation on each array element.const arr = [1, 2, 3]
arr.map((value, index, array) =>{
return value*value;
})filter() -> filters an array with values that passes a test. Creates a new arrayconst arr = [1, 2, 3, 4, 5]
arr.filter((value, index, array)=>{
return value>3;
}; reduce() -> Reduces an array to a single valueconst arr = [1, 2, 3, 4, 5]
arr.reduces(arr.reduce(add)); // add -> function
// return 1+2+3+4+5 i.e. 5Note:
Map vs forEach: map Transforms elements in an array and returns a new array., while forEach Iterates over elements to perform side effects.map, filter & return a new array, reduce returns a value and forEach & returns undefined.map()let arr = [45, 23, 21];
let newArr = arr.map((value)=>{
console.log(value+1) // 46 -> 24 -> 22
return value + 1;
})
console.log(newArr) // [46, 24, 22]let arr = [45, 23, 21];
let newArr = arr.map((value, index, array)=>{
console.log(value, index) // 45 0 -> 23 1 -> 21 2
console.lgo(array) // [45, 23, 21] -> ,,, -> ,,,
return value + index
})
console.log(newArr) // [45, 24, 23]filter()let arr = [45, 23, 21, 0, 3, 5];
let newArr = arr.filter((value)=>{
console.log(value<10) // 0 -> 3 -> 5
return value < 10;
});
console.log(newArr) // [0, 3, 5]reduces()let arr = [1, 2, 3, 5, 2, 1];
let newArr = arr.filter((val1, val2)=>{
console.log(val1+val2)// 1+2 -> 3+3 -> 6+5 -> 11+2 -> 13+1 -> 14
return value1 + value2;
});
console.log(newArr) // 14reduce method takes two values at a time (value1 and value2).value1 + value2 in this case) and returns the final accumulated value after processing all elements.const P = promise() // here promise automatically called? aur called when .then() or .catch()
then(value) and catch(error), value and error are keyword, or variable
why we writing new Promise(()=>...)
We are trying to mimic a promise, whenever you use a library like mongodb or fetch api , will return promise automatically
Await is used to Pause or wait for a specific Statement completion, because some future statement may be depend on its result
Each Async functions runs parallely, Its mean, Await will wait only for the Async Function in which it is used, Code out of the this async will run parally
Javascript is a asynchronous language, its mean if a statement takes time, it will run in background, other statements will not be affected or wait for it.
If you want to make Synchronous Sequence of Statements, or maintain flow of execution, Write all the statements as await inside an asynchronous function, each statement will wait for its previous one to execute
this in global statement in browser -> point to window this in global statement in Nodejs -> point to empty object
Implicit Return works in Single line
Explicit Return
const addTwo = (numl, num2) => return { numl + num2 }❌ Implicit Return will not work (Due to curly braces)
const addTwo = (numl, num2) => { numl + num2 }✅ Implicit Return
const addTwo = (numl, num2) => ( numl + num2 )✅ Implicit Return
const addTwo = (numl,num2) => numl + num2