Passing a global variable to a function

javascript, scope

Solution

As answered by Oriol, it doesn't work because the variable is passed by value, so you're not changing the "that" variable. A workaround would be to pass the variable name :

that = 0;

function test(input) {
    window[input]++;
}

test("that");

console.log(that); // 1

Problem

How come the following code is giving me a 0 instead of a 1? I want my function to change a variable declared outside the function but I do not want to specify the variable in the function declaration. ``` that = 0; function go(input) { input++; } go(that); console.log(that); ```

Original source

Related problems