Reputation: 31
<!DOCTYPE html>
<html>
<head>
<script>
function clickCounter() {
if(typeof(Storage) !== "undefined") {
if (localStorage.clickcount) {// <----- here!
localStorage.clickcount = Number(localStorage.clickcount)+1;
} else {
localStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = "You have clicked the button " + localStorage.clickcount + " time(s).";
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support web storage...";
}
}
</script>
</head>
<body>
<p><button onclick="clickCounter()" type="button">Click me!</button></p>
<div id="result"></div>
<p>Click the button to see the counter increase.</p>
<p>Close the browser tab (or window), and try again, and the counter will continue to count (is not reset).</p>
</body>
</html>
localStorage.clickcount
<-- it returns boolean ???
I can't understand it.
because there is no expression like ==
at all.
Upvotes: 1
Views: 43
Reputation: 508
These cases return false:
boolean is false
number is 0\NaN
string is ''
var is undefined
var is null
otherwise -> return true
Upvotes: 0
Reputation: 24280
A term that you may see often in this context is truthy. MDN describes it as follows:
In JavaScript, a truthy value is a value that is considered true when evaluated in a Boolean context.
Source: Truthy
The if-statement wants/needs a boolean value. It checks what it got actually fed, and interprets that as either true (truthy) or false (falsy).
Upvotes: 1
Reputation: 36441
ECMASCript defines rules to convert expressions to boolean values:
9.2 ToBoolean
- Number : The result is false if the argument is +0, -0, or NaN; otherwise the result is true.
Then the statement is equivalent to:
if (localStorage.clickcount != 0)
Upvotes: 4