Posts

Showing posts with the label spread operator

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' ,