CancelImage Upload

How to join two arrays together in JavaScript

To join two arrays in JavaScript the function concat() is required. This function needs to be placed at the end of the first array, with the second array contained inside it's brackets. Here is how it is implemented:

<html>
<head>
<title>How to join two arrays together in JavaScript</title>
<script type="text/javascript">

function arrayJoin()
{

       var display = document.getElementById('display');

       var fruits = new Array('Oranges', 'Pears', 'Bananas');

       var moreFruits = new Array('Strawberries', 'Apples');

       display.innerHTML = fruits.concat(moreFruits);

}

</script>
</head>
<body onload="javascript:arrayJoin();">

<span id="display"></span>

</body>
</html>

As you can see in the above example the array moreFruits is added to fruits. This combination is then loaded into the span with the id display via getElementById().

Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com