Squid Dev Squid Dev - 5 months ago 11
Javascript Question

How to change a variable that is stated in a parameter

I wanted to shorten up my code by being able to change a variable that is listed in the parameters. What I mean by this is being able change which variable is being changed by the function. How can I do this in the shortest way possible?

Current HTML and JS:

<script src="test.js"> </script>
<p onclick="TEST(this, test)"></p>


.

var test= 0;
function TEST(element, value){
element.innerHTML = value
value= 1;
}

Answer

in javascript everything is passed by value. there is no pass by reference.

You can pass an object and change properties on that.

var test= { value: 0 };
function TEST(element, value){
  element.innerHTML = value
  value.value= 1;
}