728x90
SMALL
#Javascript
#JavaScript / Object / Array.concat() / Add elements or arrays to an existing array to create a new array
.concat()
You can use the .concat() property to add elements or arrays to an existing array to create a new array.
grammar
var jbAry2 = jbAry1.concat( 'abc' );
Add the string abc to the jbAry1 array to create the jbAry2 array.
var jbAry3 = jbAry1.concat( jbAry2 );
Sum two arrays jbAry1 and jbAry2 to create a new array jbAry3.
example
The following two examples produce the same result.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript</title>
</head>
<body>
<script>
var jbAry1 = [ 'Lorem', 'Ipsum' ];
var jbAry2 = jbAry1.concat( 'Dolor', 'Amet' );
for ( var i = 0; i < jbAry2.length; i++) {
document.write( '<p>' + jbAry2[ i ] + '</p>' );
}
</script>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript</title>
</head>
<body>
<script>
var jbAry1 = [ 'Lorem', 'Ipsum' ];
var jbAry2 = [ 'Dolor', 'Amet' ];
var jbAry3 = jbAry1.concat( jbAry2 );
for ( var i = 0; i < jbAry3.length; i++) {
document.write( '<p>' + jbAry3[ i ] + '</p>' );
}
</script>
</body>
</html>
728x90
LIST