0

I wanted to check whether an item in localWebstorage was not yet given a value. I tried doing this by:

    //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 thinks that the item is not undefined");
        }
    }

This does not work, HOWEVER.. The question was asked here: How to check for "undefined" in JavaScript? and someone answered:

        if (typeof getValue != "undefined") {
            localStorage.intervalSetting = "option1";
        }

They suggested replacing === with !=
For some reason, this works- How???
Shouldn't (getValue != "undefined") return false because != means NOT EQUAL??

  • 4
    `typeof` returns a **string** – Jaromanda X Jun 04 '18 at 01:27
  • Undefined is not a string though... is it!? –  Jun 04 '18 at 01:28
  • 1
    `if (getValue === undefined)` -- no need to check the type of any vars. – ggorlen Jun 04 '18 at 01:29
  • 1
    `if (typeof getValue === undefined) {` is never true ... `if (typeof getValue != "undefined") {` is true if `getValue` is **not** undefined – Jaromanda X Jun 04 '18 at 01:29
  • [undefined](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined) is a primitive – Sterling Archer Jun 04 '18 at 01:29
  • 2
    @demonhunter24 `typeof` returns a *string* representing the *type* of its argument. The string representation of the type of the expression `undefined` is the string `"undefined"`. – nanofarad Jun 04 '18 at 01:30
  • Does this mean that my second code example is asking if getValue if NOT a string? ..and therefore pays NO attention to what the string actually says? –  Jun 04 '18 at 01:33
  • Say getvalue is indeed undefined, using typeof on it will return the string "undefined" exactly like if you declare `let myvar = "undefined";` so your if statement is never true, because "undefined" is of type string and === checks the type as well, also the string "undefined" is not undefined, there is actually a value in there which just happen to be "undefined" – Rainbow Jun 04 '18 at 01:39
  • Doing `console.log(typeof undefined);` in the browser console might shed some light on what's going on. Result is `"undefined"`. Of course `console.log(typeof typeof undefined);` is `"string"`. *Head explodes* – ggorlen Jun 04 '18 at 01:43

2 Answers2

1

In your code you compared typeof getValue with the literal type undefined. Since typeof actually gives you a string, you should compare this value to the string "undefined".

Either

if (typeof getValue !== "undefined")

Or

if (getValue !== undefined)

will do the trick.

Sangram Nandkhile
  • 17,634
  • 19
  • 82
  • 116
Martinqua
  • 11
  • 3
0

I recommend using a "truthy" check. This will check if the object is null or undefined. You can do this by just checking your getValue.

if(getValue) {
    // not undefined
}

Another SO post for reference: Understanding JavaScript Truthy and Falsy

Kody
  • 905
  • 9
  • 19
  • 2
    what if getValue is **ZERO**? ... or empty string? – Jaromanda X Jun 04 '18 at 01:30
  • This might be risky if `0` is a potential `getValue` value. `(getValue === undefined)` might be more accurate. – ggorlen Jun 04 '18 at 01:30
  • If 0 is a valid value that you want, you can add that to the condition. – Kody Jun 04 '18 at 01:31
  • Alternatively you can do what ggorlen is suggesting, `if (getValue === undefined) {...}`, but that will not account for null values. – Kody Jun 04 '18 at 01:34
  • 1
    OP isn't asking to check for `null`. The question is specific about "not having been given a value", which is `undefined`. `0`, `""` and `null` all need to be explicitly set on a var AFAIK. – ggorlen Jun 04 '18 at 01:40
  • Checking for undefined explicitly and disregarding other potential "non-value" values is just begging for problems. You are free to do as you want, but what you want is not always what you need. – Kody Jun 04 '18 at 20:17