Using a global variable in JavaScript
By using var
when setting number = '10', you are declaring number
as a local variable each time. Try this:
var number = null;
function playSong(artist, title, song, id)
{
alert('old number was: ' + [number] + '');
number = '10';
alert('' + [number] + '');
}
Remove the var
in front of number
in your function. You are creating a local variable by
var number = 10;
You just need
number = 10;
The problem is that you're declaring a new variable named number
inside of the function. This new variable hides the global number
variable, so the line number = 10
assigns only to this new local variable.
You need to remove the var
keyword from var number = 10
.