Mastering the push() Method in JavaScript โž•

Mastering the push() Method in JavaScript โž•
The push() method in JavaScript allows you to add one or more elements to the end of an array and then returns the new length of the array. It's considered a mutating method because it changes the original array.

๐Ÿ” Try this example in the console yourself: 
Press `F12` to open the developer tools, navigate to the โ€˜consoleโ€™ tab, and paste the code provided below. Press `Enter`, change the numbers, and play around with the code to see different results!

<pre class="preFormatted"><code class="codeFormatted">
const array1 = [1, 2, 3, 4];
const newLength = array1.push(5, 6);
console.log(array1); 
// output: [1, 2, 3, 4, 5, 6];
</code></pre>

As you can see, the `push()` method adds the elements 5 and 6 to the original array and returns the updated array!

Published: April 2024

โ† Back to Blog