1

I am new to JavaScript and I am making and idle game and what I want to do is to change the value of a variable inside a function wich is called by an event listener like this:

const foo = (x) =>{
    x = x + 1;
}
foofoo.addEventListener("click", function(){foo(x)})

where x is a variable is this possible?

decod decod
  • 109
  • 8
  • 1
    `x` is passed by value, so it's a copy. Use an object, return a value or make it global (the second option is by far the best). – ggorlen Apr 16 '19 at 15:27
  • 1
    `let x; ... const foo = () => { x++ };`? – p.s.w.g Apr 16 '19 at 15:27
  • @ggorlen I am running this from an eventlistener how can I do that? – decod decod Apr 16 '19 at 17:08
  • Edit your question to provide a [mcve] that reproduces what you're trying to accomplish, otherwise it's a bit hard to say in a helpful way that wouldn't be overly general. – ggorlen Apr 16 '19 at 17:12

0 Answers0