I was trying to determine if a value had been assigned to an item in localWebstorage. My initial approach involved the following code snippet:
//Change localStorage.intervalSetting from 'Undefined' to 'Never'
function initialIntervalSetting() {
var getValue = localStorage.intervalSetting;
if (typeof getValue === undefined) {
localStorage.intervalSetting = "option1";
alert("Changed the localWebstorage item from undefined to 'option1'");
}
else {
alert("JavaScript believes that the item is not undefined");
}
}
Even though this method did not yield the desired results, I stumbled upon a valuable discussion on the matter at: How to check for "undefined" in JavaScript?. A user shared the following solution:
if (typeof getValue != "undefined") {
localStorage.intervalSetting = "option1";
}
The advice given was to use != instead of ===. Surprisingly, making this change solved the issue. But why does it work? Shouldn't (getValue != "undefined") evaluate to false since != signifies NOT EQUAL??