Sudip
Sudip

Reputation: 1

Storing arugement of function from within function in javascript

Try it

Try it again

JAVASCRIPT

var ages = [32, 33, 16, 40];

function checkAdult(age) {
return age >= 18;
}

function myFunction(dd) {
dd=dd.filter(checkAdult)
document.getElementById("demo").innerHTML = dd.filter(checkAdult);
}
function myFunction1() {
document.getElementById("demo").innerHTML = ages
}

try it button will give me [32.33.40] after that when i press "try it" again button , i don't get [32,33,40] instead i get [32,33,16,40] , Could i get [32,33,40] from second button, when it is clicked after first button

Upvotes: 0

Views: 58

Answers (1)

Damon
Damon

Reputation: 4336

There is no need to pass z as an argument if it is available in the outer scope.

var z = 0
function myFunction(x, y) {
  console.log('z before', z)
  z = x * y
  document.getElementById('demo').innerHTML = z
  console.log('z after', z)
}
<button onclick="myFunction(2,3)">2*3</button>
<button onclick="myFunction(4,5)">4*5</button>

<div id="demo">0</div>

As you can see, z in the outer scope will be updated from within myFunction.

Upvotes: 1

Related Questions