Asynchronous JavaScript


Once you get the hang of javascript promises, you might like async await, which is just "syntactic sugar" on topof promises. In the following example we create an async function and within that we await the greeter promise.

map, filter, reduce

There is some confusion around the javascript array methods map, filter, reduce. These are helpful methods for transforming an array or returning an aggregate value.

map: return array where each element is transformed as specified by the function

const arr = [1, 2, 3, 4, 5, 6]; const mapped = arr.map(el => el + 20); console.log(mapped); // [21, 22, 23, 24, 25, 26]

filter: return array of elements where the function returns true

const arr = [1, 2, 3, 4, 5, 6]; const filtered = arr.filter(el => el === 2 || el === 4); console.log(filtered); // [2, 4]

reduce: accumulate values as specified in function

const arr = [1, 2, 3, 4, 5, 6]; const reduced = arr.reduce((total, current) => total + current, 0); console.log(reduced); // 21
                

Console


const marks = [80, 90, 70, 60, 50] const names = ['rabin', 'shyam', 'sita', 'sita', 'sita'] const mixed = ['str', 10, true, [10, 20, 30]] let arr = new Array(10, 20, 30, 40, 50) console.log(arr.indexOf(20)) /// Mutating arr.push(89) // last of array arr.unshift(100) // front of array arr.pop() // remove last element arr.shift() // remove first element arr.splice(2, 1) // remove element from index 2 arr.splice(2, 0, 'rabin') // add element at index 2 arr.reverse() // reverse array arr.sort() // sort array arr.sort((a, b) => a - b) // sort array in ascending order arr.sort((a, b) => b - a) // sort array in descending order arr.concat(names) // concat array console.log(arr.concat(names)) // concat array // object // let myObj = { 'full name': 'rabin', age: 23, channel: 'youtube', address: { city: 'kolkata', state: 'WB', country: 'India' }, marks: [80, 90, 70, 60, 50], } console.log(myObj.address.city) console.log(myObj['full name']);