Posts

Showing posts from July, 2020

What is object destructuring and spread operator in JavaScript?

Object destructuring Object destructuring is a feature in JavaScript which allows us to assign values from arrays and properties from objects to distinct variables. This is also referred to as destructuring assignment . Examples Swapping two numbers let   x  =  5 ,  y  =  7 ; [ x ,  y ] = [ y ,  x ]; console . log ( x ,  y );  // 7 5 Taking out values from an array and assigning them to variables let   array  = [ 1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ]; const  [,  a , , ,  b ,  c ] =  array ; console . log ( a ,  b ,  c );  // 2 5 6 Assigning remaining values to a variable let   array  = [ 1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,  9 ]; const  [ x , ... y ] =  array ; console . log ( x ); console . log ( y ); // 1 // [ //   2, 3, 4, 5, //   6, 7, 8, 9 // ] Taking out properties from an object and assigning them to variables const   student  = {      name:   'Student 1' ,      rollNo:   45 ,      subject:   'Economics' ,   

What are filter, map and reduce functions in JavaScript?

These are in built functions which are available on arrays in JavaScript. In this article, we will talk about what these functions do and how they are used. filter – Array.prototype.filter This function returns a subset of an array containing those elements which satisfy a given condition in the form of a callback . Syntax newArray = someArray.filter(callback(element[, index, array])[, thisArg]) newArray is the filtered array. someArray is the original array. callback is the callback function. element is the current element of the array which is being processed by the filter function. index is the index of the current element. array is the original array. thisArg is the value to be used as this while executing the callback. If the callback function returns a truthy value then the element will be included in the new array otherwise it will be ignored. Examples Getting even numbers from a list of numbers let   numbers  = [ 1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,  9 ,