最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can I use (undefined || null) to check if my variable is null or undefined? - Stack Overflow

programmeradmin0浏览0评论

In my Javascript code I'm checking if a variable is undefined (not the value undefined but the variable not defined) or null. To trim the code I'm using an operator. This is how I do it:

if (myVariable === (undefined || null)) {
    // Do something.
}

A friend of mine told me once, that I should rather split the checks into:

if (myVariable === undefined || myVariable === null) {
    // Do something.
}

Is there really any difference between these two approaches? If yes, which one should I use and why?

In my Javascript code I'm checking if a variable is undefined (not the value undefined but the variable not defined) or null. To trim the code I'm using an operator. This is how I do it:

if (myVariable === (undefined || null)) {
    // Do something.
}

A friend of mine told me once, that I should rather split the checks into:

if (myVariable === undefined || myVariable === null) {
    // Do something.
}

Is there really any difference between these two approaches? If yes, which one should I use and why?

Share Improve this question edited Jul 21, 2014 at 15:20 user2226755 13.2k6 gold badges53 silver badges75 bronze badges asked Jul 18, 2014 at 7:11 dwettsteindwettstein 6871 gold badge7 silver badges17 bronze badges 11
  • 2 Lol... No, this won't work. Write a helper function like this: function IsNullOrUndefined (obj) { return obj === null || typeof obj === 'undefined'; } – ˈvɔlə Commented Jul 18, 2014 at 7:20
  • @WoIIe: "this won't work" -- what "this"? The second will. – zerkms Commented Jul 18, 2014 at 7:21
  • @zerkms Just everything ... The check if a myVariable === undefined doesn't check if myVariable is undefined. And since (undefined || true) will always return false, he is just checkinf if myvariable === false ... – ˈvɔlə Commented Jul 18, 2014 at 7:22
  • @Wolle—"that" won't work either. If a variable hasn't been declared or initialised, attempting to access it's value will throw an error in the call to your function. – RobG Commented Jul 18, 2014 at 7:22
  • @WoIIe: "The check if a variable === undefined won't work" --- why do you think so? What if you try before you continue this pointless discussion? – zerkms Commented Jul 18, 2014 at 7:22
 |  Show 6 more comments

5 Answers 5

Reset to default 11

Is there really any difference between these two approaches?

Yes.

myVariable === (undefined || null)

is equivalent to

myVariable === null

which is only true if myVariable is null, and false if myVariable is undefined. Whereas:

myVariable === undefined || myVariable === null

returns true if myVariable is either undefined or null.

If yes, which one should I use and why?

Neither (probably), even if the answer was yes. If you are trying to determine whether a variable exists or not, you can only test for global variables as they are properties of the global object:

// In global code
var window = this;

// Later…
if (varname in window) {
  // varname is a global variable or property
}

Within a function execution context, you can only reliably test for a variable using try..catch:

try {
  var blah = foo;
} catch (e) {
  // foo is probably not a variable in scope
}

But that is almost certainly not a good idea. See JavaScript check if variable exists (is defined/initialized) - Which method is better?.

You should probably be doing:

if (typeof varname == 'undefined' || varname === null) {
  // varname either does't exist or has a value of undefined or null.
}

The tests need to be in that order so that if varname hasn't been declared or otherwise created, the typeof test fails before the null test, which would otherwise throw an error.

Prefere : if (typeof myVariable === "undefined" || myVariable === null) {.

variable === undefined vs. typeof variable === "undefined"

Because with if (myVariable === undefined) { your console can be return an error or warning.

Like this :

ReferenceError: myVariable is not defined
    if (myVariable === undefined) {

PS : (undefined || null) is always null (because undefined return false).

=== operator in JS compares 2 operands (values).

In case of myVariable === (undefined || null) the operands are: myVariable, which represents the value it holds, and (undefined || null) which represents the value null, because operands (expressions) must be evaluated before comparison. And the (undefined || null) expression is evaluated to null.

So effectively your solution is identical to myVariable === null.

If you follow the same idea and evaluate your friend proposal you will see that his advice is correct.

It's because (undefined || null) always evaluates to null so your first expression always false when myVariable is undefined. The second variant is do what you want correctly.

Yes, it is. For your example, undefined is equal false then null is equal false too and this last value returns from expression. So this is why first approach is equal to if (myVariable === null) { ... }. The second approach is preferable, but if you not a 'JavaScript: The Good Parts' guy, you can stick with if (myVariable == null) { ... } or if (myVariable == undefined) { ... }.

发布评论

评论列表(0)

  1. 暂无评论