1.

How and why to use leading bang! in JavaScript function?

Answer»

Let’s say the following are our arrays:

[20, 40, 60] [80, 100, 120]

To merge them, JavaScript provides the Array.concat() method. The return value of this method is a new array with the result of concatenated arrays: 

var res = arr1.concat(ARR2);

Let us now merge the above two arrays: 

&LT;html>    <head>       <title>Merge JavaScript Arrays</title>    </head>    <BODY>       <script>          var arr1 = [20, 40, 60];          var arr2 = [80, 100, 120];          document.write("Array 1 = " + arr1);          document.write("<br>Array 2 = " + arr2 );          var res = arr1.concat(arr2);          document.write("<br>Merged Arrays = " + res );       </script>    </body> </html>

The OUTPUT displays the merged arrays:

Array 1 = 20,40,60 Array 2 = 80,100,120 Merged Arrays = 20,40,60,80,100,120


Discussion

No Comment Found