JAVASCRIPT Tutorial

Push in JS

Concept:

The push() method is used to append one or more elements to the end of an array.

Elements:
  • array: The array to be modified.
  • Element: The value(s) to be added to the array.
Modifies Original Array:

Yes, push() directly modifies the original array.

Practical Steps:

  1. Declare an array.
  2. Call the push() method on the array.
  3. Pass the elements to be added as arguments to the push() method.

JavaScript Example:

const arr = [1, 2, 3];
arr.push(4, 5); // Add 4 and 5 to the end of the array
console.log(arr); // Output: [1, 2, 3, 4, 5]

Additional Notes:

  • The push() method returns the new length of the array.
  • To add a single element, pass it directly to push().
  • To add multiple elements, pass them as separate arguments or within an array.