Pass an array to a function in JavaScript

Description

The following code shows how to pass an array to a function.

Example


<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function makeArray() {<!--from   w ww  .j a v  a 2  s.  c o  m-->
var myArray = new Array(4);
myArray[0] = "A";
myArray[1] = "B";
myArray[2] = "C";
myArray[3] = "D";
return myArray;
}
function showArray(theArray){
var quote = "";
for (var i = 0; i < theArray.length; i++){
quote += theArray[i] + " ";
}
return quote;
}
var x = makeArray();
document.write(showArray(x));
</script>
</head>
<body>
</body>
</html>

Click to view the demo

The code above generates the following result.

Pass an array to a function in JavaScript