Javascript Array argument to function by reference


If you include a javascript array as an argument, it is passed by reference. So any change you make inside the function affects the elements of the array.

Take a look at the following example


<html>
<body>

<script type="text/javascript">

function myfunction(refArray) {
   refArray[1] = 10000;
}

var a = [1,2,3,4,5,6,7,8,9,10];
document.writeln(a+'<BR>'); // outputs 1,2,3,4,5,6,7,8,9,10
myfunction(a);
document.writeln(a+'<BR>'); // outputs: 1,10000,3,4,5,6,7,8,9,10

</script>

</body>
</html>



Try this example online here .