Skip to content

JavaScript multidimensional array push value | Code

  • by

Use the push() method to add one or more elements to the end of a multidimensional array in JavaScript. This method returns the new length of the array.

arrayA.push(arrayZ);

JavaScript multidimensional array push

A simple example code will add it to the end of the array.

<!DOCTYPE html>
<html>
<body>

  <script>
    var z = new Array(7, 8, 9);
    var a = new Array(
      [1, 2, 3],
      [4, 5, 6]
      );

    console.log(a.push(z));
    console.log(a)
  </script>

</body>
</html> 

Output:

JavaScript multidimensional array push

On ES6 you can use the spread operator (...) as follows:

<script>
    arrayA = [
    [1, 2, 3],
    [4, 5, 6]
    ];

    arrayB = [7,8,9];

    arrayA = [...arrayA, ...[arrayB]];
    console.log(arrayA)
</script>

Comment if you have any doubts or suggestions on this JS Array topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading