Ryan Schaefer
Ryan Schaefer

Reputation: 3120

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;
}

Upvotes: 0

Views: 69

Answers (1)

Daniel A. White
Daniel A. White

Reputation: 190945

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;
}

Upvotes: 1

Related Questions