HTML and CSS Reference
In-Depth Information
The concat() Method. The concat() method concatenates the elements passed as
arguments onto an existing array (JavaScript 1.2), returning a new concatenated list.
The method does not change the existing array in place. You must assign results to either
an existing array or a new one.
FORMAT
newArray=oldArray.concat(new elements);
EXAMPLE
names = names.concat("green, "blue");
EXAMPLE 9.10
<html>
<head>
<title>Array concat() methods</title>
</head>
<body>
<script type="text/javascript">
1
var names1=new Array("Dan", "Liz", "Jody" );
2
var names2=new Array("Tom", "Suzanne");
document.write("<b>First array: "+ names1 + "<br />");
document.write("<b>Second array: "+ names2 + "<br />");
document.write("<b>After the concatenation <br />");
3
names1 = names1.concat( names2);
document.write(names1);
</script>
</body>
</html>
EXPLANATION
1
The first Array object, called names1 , is created.
2
The second Array object, called names2 , is created.
3
After concatenating the names2 array to names1 , the result is returned written to
names1 (see Figure 9.13). Without assigning the return value to names1 , names1
will not be changed. You can return the results to a completely different array. The
concat() method allows the elements of one array to be added to another.
Figure 9.13 The Array concat() method before and after.
 
Search WWH ::




Custom Search