Link to home
Start Free TrialLog in
Avatar of Charlie_S24

asked on

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

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.

<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
  var x=document.getElementById("demo");
function myOtherFunction(aryDifferentArray)
  aryDifferentArray[0][1] = 4
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial