Javascript (Important)

1/1/1970

Javascript (Important)

Map, Filter & Reduce in JavaScript | JavaScript Tutorial in Hindi #20 (Harry)

Looping through Arrays

Arrays can be looped through using the classical javascript for loop or through some other methods discussed below

  1. forEach loop -> Calls a function, once for each element
const arr = [1, 2, 3]
arr.forEach((value, index, array)=>{
	//function logic
})
  1. 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;
})
  1. filter() -> filters an array with values that passes a test. Creates a new array
const arr = [1, 2, 3, 4, 5]
arr.filter((value, index, array)=>{
	return value>3;
}; 
  1. reduce() -> Reduces an array to a single value
const arr = [1, 2, 3, 4, 5]
arr.reduces(arr.reduce(add)); // add -> function 
// return 1+2+3+4+5 i.e. 5

Note:

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) // 14

Code With Harry JS PDF Notes


# Introduction to Callbacks | JavaScript Tutorial in Hindi #52(Harry)


# Callback Hell & Pyramid of Doom | JavaScript Tutorial in Hindi #53(Harry)


Introduction to Promises | JavaScript Tutorial in Hindi #54(Harry)

const P = promise() // here promise automatically called? aur called when .then() or .catch()

Promise .then() and .catch() | JavaScript Tutorial in Hindi #55

then(value) and catch(error), value and error are keyword, or variable

Promise Chaining .then() calls | JavaScript Tutorial in Hindi #56

Attaching Multiple Handlers to a Promise | JavaScript Tutorial in Hindi #57

The Promise API | JavaScript Tutorial in Hindi #58

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


Async/Await in JavaScript | JavaScript Tutorial in Hindi #59(Harry)

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


Error Handling: try and catch | JavaScript Tutorial in Hindi #60 (Harry)


THIS and arrow function in javascript | chai aur javascript(Chai aur Code)

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

Immediately Invoked Function Expressions IIFE (Chai aur Code)