I'm just beginning with javascript, but I've searched and read about the js variablescope, and cant figure this out.
I want to be able to set a variable value declared outside any function, within the functions wich also contains if-statements. But yet it does not change the global variable. Example:
var counter = 0;
goNext();
function goNext()
{
if(counter = 0)
{
counter = 3;
}
}
alert('I now want counter to be 3! How?');
Thank you in advance, dedicated users.
Inside the if you are assigning 0 to counter
if(counter = 0)
The code needs to be
if(counter == 0)
or
if(counter === 0)
Using a tool like JSHint will hlp you find these errors. By pasting your code into the interface, it will show you warnings. When I paste your code into the page it produced:
One warning
6 Expected a conditional expression and instead saw an assignment.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.