Why is my local Javascript array's being modified in external functions?

Charlie_S24
Charlie_S24 used Ask the Experts™
on
Although I thought I understood how local and public variables work with JavaScript, I still have a function modifying my two-dimensional array.  I'm not sure how to prevent that from happening.  (Have I declared the array properly?  Am I passing it properly?)

My goal is to have a two-dimensional array in the main routine whose data is passed to a function that uses the data, but leaves the original array in the main routine untouched.

Below is a little bit of fictitious code that duplicates the problem.  

Your help would be appreciated.

<html>
<body>
 
<p id="demo">Click the button to test code.</p>
 
<button onclick="myFunction()">Try it</button>
 
<script type="text/javascript">

function myFunction()
{
  var aryFirstArray =  new Array(1)
  aryFirstArray[0] = new Array()
  aryFirstArray[0][0] = "I am expecting the 2nd dimension to still be 5: "
  aryFirstArray[0][1] = 5
  myOtherFunction(aryFirstArray)
 
  var x=document.getElementById("demo");
  x.innerHTML=aryFirstArray;
}
function myOtherFunction(aryDifferentArray)
{
  aryDifferentArray[0][1] = 4
}
</script>
</body>
</html>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
IT Expert
Top Expert 2009
Commented:

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial