Reputation: 23
if localStorage["BestScore"] = undefined;
{
localStorage["BestScore"]=0;
maxScore=0;
}
var maxScore=localStorage["BestScore"];
var newScore=false
function drawScore(score) {
if (newScore == true && score < maxScore) {
newScore = false;
}
if (score > maxScore) {
newScore = true;
localStorage["BestScore"] = score;
if ([5, 10, 15, 20].indexOf(score) !== -1) {
play(sndMedal);
} else {
play(sndGain);
}
}
This code is to set the max score and then store it but it doesn't seem to set the local storage to 0 if undefined.
Upvotes: 1
Views: 506
Reputation: 5614
However if you need to check a variable against undefined
value, there is no need to invent any special method, since JavaScript has a typeof
operator, which is simple, fast and cross-platform:
if (typeof localStorage["BestScore"] === "undefined") {
localStorage["BestScore"] = 0;
}
It returns a string indicating the type of the variable or other unevaluated operand. The main advantage of this method, compared to if (value === undefined) { ... }
, is that typeof
will never raise an exception in case if variable value
does not exist.
Upvotes: 2
Reputation: 4530
if localStorage["BestScore"] = undefined;
should be:
if( typeof localStorage["BestScore"] === 'undefined' )
Upvotes: 3
Reputation: 714
use ==
or ===
as a comparison operator, then it should be fine
Upvotes: 0
Reputation: 34628
if localStorage["BestScore"] = undefined;
should be ==
else you are assigning, not comparing.
Upvotes: 2