How do you push multiple objects into an array?

 There are different ways to push the elements into a single array using JavaScript.

  1. Push()
  2. Unshift()
  3. Spread operator

1. Push(): This operator add multiple items in end of an array.

# Lets take an empty array

  1. let array = []; 

#add elements using push operator

  1. array.push(5); 
  2. array.push(10); 

# Print an array using

  1. console.log(array); 

Output: (2) [5,10]

2. Unshift() : To add multiple objects at the start of an array.

  1. let array = []; 
  2. array.unshift(10); 
  3. array.unshift(5); 
  4. console.log(array); 

Output: (2) [5,10]

3. Spread Operator: This operator helps you copy one array into another array.

  1. let array1 = [1, 2, 3]; 
  2. let array2 = ['Hello', ...arr1, 'World']; 
  3. console.log(arr2); 

In this operator if you are not use three dot it throw an error.

Comments

Popular posts from this blog

proassignment help backlink data index 41

programming shark backink data index 31

Matlabworks backlink data index 86