4

Is there anything wrong with this test for undefined?

var undefined;

if(x == undefined){
    //do something
}

or this:

function undefined(x){
    return typeof x == 'undefined';
}

if(undefined(x)){
    //do something
}

jsLint doesn't throws a reserverd word error, but the code still seems to work...

Mark Brown
  • 12,026
  • 8
  • 27
  • 32
  • what's wrong with simply testing if (typeof(x) === 'undefined') { ... } – Tim O May 20 '11 at 01:58
  • @Tim O Nothing, but 'x == undefined' or undefined(x) is much shorter. And I end up testing for undefined often. Just trying to make my life a bit easier... – Mark Brown May 20 '11 at 02:02

4 Answers4

7

Don't redefine undefined.

Other programmers expect undefined to always be undefined, not a function for function's sake.

People often use typeof operator to ensure a reference error is not thrown when used to test for variables that are undefined.

If anyone ever does this to you, you can use...

undefined = void 0;

... to revert it back.

Community
  • 1
  • 1
alex
  • 479,566
  • 201
  • 878
  • 984
  • 1
    Of course, some people go on the defensive and use the `void` operator to test: `if (a === void 0)`. – Reid May 20 '11 at 02:03
  • If you want your own function for this just name it `isUndefined()` or similar, to avoid clobbering the normal `undefined`. – nnnnnn May 20 '11 at 03:40
  • 1
    To clarify, the `var undefined` part is fine, but making it into a function is not. – Tyler May 20 '11 at 06:16
5

As undefined isn't a Javascript keyword, there's nothing wrong with it per se.

However, you're overriding a core variable that's used frequently for checking undefined variables in your second example to be a function. I'd shriek and ban that person as a committer if I saw that in anyone's code that I was reviewing.

Demian Brecht
  • 21,135
  • 5
  • 42
  • 46
1

undefined is just a default property of the global object, which you can override/redefine. That's why you should always test for undefined using typeof x == 'undefined', since the typeof operator cannot be redefined.

var undefined;

if(x == undefined){
    //do something
}

What happening here is that you're defining a new variable called "undefined", which you don't assign a value and which hence gets the valued undefined. x is not defined either and also has a value of undefined. Hence both are equal. It's rather pointless though.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Yes. I understand why it works. I'm just trying to figure out why I shouldn't do it according to js lint – Mark Brown May 20 '11 at 02:05
  • @Mark Redefining `undefined` as in the first example is obviously pointless. Redefining it as a function breaks code that checks against `x == undefined` (which you shouldn't do to begin with, but hey...). – deceze May 20 '11 at 02:08
  • I definitely wouldn't use them together, but I see your point. From the responses so far I can see that it's a bad idea. – Mark Brown May 20 '11 at 02:11
1

undefined is not a reserved word in JavaScript (ECMA-262). It is a named constant of type Undefined;

By declaring:

var undefined;

you declare variable with the same name in local scope.

So technically you can do this, just don't define something like this:

var undefined = 13;
c-smile
  • 26,734
  • 7
  • 59
  • 86