concat() Method

The concat() method returns the array resulting from appending its arguments to
 the array on which it was invoked. Given the script:


var myArray = ["red", "green", "blue"];

alert(myArray.concat("cyan", "yellow")); 
 
Be careful, though; concat() does not modify the array in place. Notice the output of this script,
var myArray = ["red", "green", "blue"]; myArray.concat("cyan", "yellow"); alert(myArray);
Unlike with the push() and shift() methods discussed earlier, you will need to save the returned value; for example:
var myArray = ["red", "green", "blue"];

myArray = myArray.concat("cyan", "yellow");

If any argument to concat() is itself an array, it is flattened into array elements. This flattening is not recursive, so an array argument that contains an array element has only its outer array flattened. An example illustrates this behavior more clearly:
var myArray = ["red", "green", "blue"];

myArray.concat("pink", ["purple", "black"]);  

// Returns ["red", "green", "blue", "pink", "purple", "black"]

myArray.concat("white", ["gray", ["orange", "magenta"]]);  

// Returns ["red", "green", "blue", "white", "gray", ["orange", "magenta"]]

alert(myArray[myArray.length-1]);

// shows orange, magenta

Note 
You may notice that arrays are recursively flattened if you output the entire array with an alert. However, access the length property or the individual elements and it will become apparent that you have nested arrays.

 
Related Posts:
  • Javascript search search(regexp)The search() method is the same as indexOf() except that it takes aregular expression pattern instead of a substring. It also returns -1 if thepattern isn’t found."hello world".search(/[aeiou]/); // Find the fi… Read More
  • Javascript The + Operator The binary + operator adds numeric operands or concatenates string operands: 1 + 2 // => 3"hello" + " " + "there" // => "hello there""1" + "2" // => "12" When the values of both operands are numbers, or are both str… Read More
  • JavaScript Variables Variables store a value you can refer to later in the script. Variable namescan be nearly any valid identifi er. A JavaScript identifi er is a word thatcontains only letters, numbers, $, and _, and that doesn’t start with an… Read More
  • JavaScript String Split Function split()The split() method splits (for lack of a better word) a string up into substrings and returns them in an array. It accepts a string or regular expression argument containing the delimiter at which the string will be b… Read More
  • Creating A Function In Javascript The basic structure of a function looks like this: function functionName() {// the JavaScript you want to run} The keyword function lets the JavaScript interpreter know you’re creating a function—it’s similar to how you use … Read More
  • Javascript String Constructor String is the built-in object corresponding to the primitive string data type. It contains a very large number of methods for string manipulation and examination, substring extraction, and even conversion of strings to marke… Read More
  • JavaScript Redirect <script type="text/javascript"> window.location = "http://www.yoursite.com/" </script> … Read More
  • Javascript Do While Loops There’s another, less common type of loop, known as a do/while loop. This type ofloop works nearly identically to a while loop. Its basic structure looks like this: do {// javascript to repeat} while (condition) ; do {var l… Read More
  • Javascript Making a simple MouseOver <a href="http://www.cit.cornell.edu"onMouseOver="document.logo.src='family.gif ' ; "onMouseOut ="document.logo.src='harry.gif ' ; " ><img name="logo" src="harry.gif " border=0></a> The onMouseOver and onM… Read More
  • JavaScript Popups <head><script type="text/javascript">function testPopup() {window.open( "http://www.google.com/" )}</script></head><body><form><input type="button" onClick="testPopup()" value="Click"&g… Read More
  • Javascript Image Objects Properties of Image Objects Property Description align Indicates the alignment of the image, usually “left” or “right.” alt The alternative text rendering for the image as set by the alt attribu… Read More
  • JavaScript System Dialogs alert(),confirm(),prompt() The browser is capable of invoking system dialogs to display to the user through the alert(),confirm(), and prompt() methods. These dialogs are not related to the web page being displayedin the browser and do not contain HTM… Read More
  • JavaScript Alert The JavaScript alert is a dialogue box that pops up and takes the focus away from the current window and forces the web browser to read the message. 1. <form> <input type="button" onclick= "alert('Are you sure you… Read More
  • Javascript Objects as Arguments You may want to write functions that take many arguments, some ofwhich are optional.function drawElement( color, border, width, height,left, top, zIndex) {// Make and display an element with these variables}drawElement("red"… Read More
  • Javascript Recursion Recursion is when a function calls itself. This is often useful in mathematics,such as fi nding the nth number in the Fibonacci series (1, 2, 3, 5,8, 13, 21…).function fi bonacci(n) {if ( n < 2 ) {return 1;} else {return … Read More