-2

When I want to make sure a variable x is defined, before using it, I use:

if (typeof x !== "undefined") {
    // do stuff with x
}

but I've noticed that others, e.g. in this question, use !== instead of != for the comparison. Which one should I be using, and why?

Note: I realize I can be using !==. The question is whether I should (and whether there will be any difference in behavior).

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Does this answer your question? [Which equals operator (== vs ===) should be used in JavaScript comparisons?](https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons) – Alessio Cantarella Jan 25 '20 at 19:01
  • @funnydman: Not quite. I don't know if type conversion in meaningful here or not. – einpoklum Jan 25 '20 at 19:01
  • Personally I'd use `if (x != undefined)` because that will also check for `null`, and in most programming situations `null` and `undefined` are pretty much equivalent. – Pointy Jan 25 '20 at 19:01
  • This might help you https://stackoverflow.com/questions/1094531/when-should-you-use-vs-vs-etc-in-javascript – sagar1025 Jan 25 '20 at 19:02
  • 1
    In your situation - doesn't matter one bit. `typeof` always returns a string, so whether you use strict equality or not, there won't be any difference. – VLAZ Jan 25 '20 at 19:02
  • 1
    @Pointy: That will fail if the variable has never been declared. – einpoklum Jan 25 '20 at 19:02
  • @sagar1025: It didn't. – einpoklum Jan 25 '20 at 19:03
  • @einpoklum well that's true, but to me having to worry about that indicates a situation that I'd rather not be in. I suppose if you have to deal with 3rd party scripts it's a thing to worry about. – Pointy Jan 25 '20 at 19:03
  • In any case VLAZ is correct, if you're checking the value of a `typeof` operation it doesn't matter. – Pointy Jan 25 '20 at 19:04
  • @Pointy: Make that an answer please. – einpoklum Jan 25 '20 at 19:05

2 Answers2

2

As noted in a comment by VLAZ, the typeof operator is guaranteed to return a string. If you compare the result to another string, then == and === (or != and !==) will do the exact same thing.

Pointy
  • 405,095
  • 59
  • 585
  • 614
0

actually, the best approach would be to check if a value is falsy, and based on MDN this is the list of falsy values:

  • false The keyword false
  • 0 The number zero
  • 0n BigInt, when used as a boolean, follows the same rule as a Number. 0n is falsy. "", '', ``
  • This is an empty string (the length of the string is zero). Strings in JavaScript can be defined with double quotes "", single quotes '', or Template literals ``.

  • null null - the absence of any value

  • undefined undefined - the primitive value
  • NaN NaN - not a number

so based on your code what you can do is simply:

if (!x) { // check for all the falsy values.
    // do stuff with x
}

in the other hand, you ask for the difference of != and !==, well basically taking some examples you can see the difference:

0 == false   // true, because false is equivalent of 0

0 === false  // false, because both operands are of different type

2 == "2"     // true, auto type coercion, string converted into number

2 === "2"    // false, since both operands are not of same type

as mentioned by @VLAZ comment, these cases will only work if the variable x is defined, otherwise you will have the following error:

"Uncaught ReferenceError: x is not defined"

so on your case you could only use != because you will compare string vs string and you will avoid having to check if the variable was or not created.

if (typeof x != "undefined") {
    // do stuff with x
}
Prince Hernandez
  • 3,623
  • 1
  • 10
  • 19
  • `if(!x)` will 1. catch `false` or `0` which might be legitimate values. If you want to check for `undefined`, then *check for `undefined`*. Broad checks have produced more bugs in my experience than saved time. Sure, you spent few seconds writing that code, maybe even few minutes actually thinking and designing what the code would and should do. But then you cost something like 10+ man hours of somebody finding out that `0` is somehow treated not the way it should or something, investigating, writing a ticket, somebody taking it, investigating, then reverse engineering the intention. – VLAZ Jan 25 '20 at 19:18
  • 1
    In addition, `if(!x)` will throw a ReferenceError if `x` is not defined at all. Something OP explicitly mentions is a possibility. – VLAZ Jan 25 '20 at 19:19
  • yes @VLAZ I edited the answer with a note for the case that `x` is not defined. related to you first comment, I know that about checking for something explicitly, but usually I described the case for checking a falsy value, I can not create an scenario for each type of variable that he wants to check, I think that is the responsibility of the developer, as you mentioned, each case will have different checks, but this was more of a general answer. – Prince Hernandez Jan 25 '20 at 19:42
  • Only your last paragraph is a valid answer to my question, and the rest is relevant to other questions... – einpoklum Jan 25 '20 at 19:53