2

Does jQuery provide a way to to that test in a more concise way?

Update I should have left jQuery out of this. It is just an issue of JavaScript. It's been so long for me! I am testing to see is something externally-defined module was loaded or not. It appears that I can just use this instead:

if (window.someVar)

Please correct me if this is a bad practice. In my case, if someVar is defined, it will be an object. It will not be defined to false.

Fantius
  • 3,806
  • 5
  • 32
  • 49

5 Answers5

6

This answer must be at least 30 characters and the answer is: No.

Greg
  • 21,235
  • 17
  • 84
  • 107
mVChr
  • 49,587
  • 11
  • 107
  • 104
1

If it is acceptable in your code to consider null and undefined to be equal, you could avoid the typeof by doing an == test on null, which will also be true for undefined.

if( someVar == null ) {
    // it was either null or undefined 
}
user113716
  • 318,772
  • 63
  • 451
  • 440
  • @Raynos: I assume OP is using `typeof` to avoid the possible issues with `undefined` being redefined. My *personal* opinion is that if `undefined` has been defined, the code *should* break. – user113716 Feb 13 '11 at 16:14
  • 1
    @patrickdw I assume the OP didn't know better. Use closures to get your own copy of `undefined`. The common use-case of `typeof` is to test for undeclared variables. – Raynos Feb 13 '11 at 16:15
  • @Raynos: Yes, I would prefer to define an `undefined` parameter as well. – user113716 Feb 13 '11 at 16:17
  • @Raynos: Maybe it's just me, but it seems like if you try to use an undeclared variable, your code should break. At least with the redefining issue, the cause could be someone else's code. – user113716 Feb 13 '11 at 16:21
  • @patrickdw there's nothing wrong with checking for `undefined` it's perfectly acceptable. you could also check for `void 0` – Raynos Feb 13 '11 at 16:23
  • @Raynos: When did I say that there's something wrong with checking for `undefined`? My point was that I assumed OP was using `typeof` for a reason. – user113716 Feb 13 '11 at 16:24
  • Not really. I didn't know that if( someVar == null ) was equivalent in most cases. – Fantius Feb 13 '11 at 16:26
  • @patrickdw Nowhere as it turned out. Yes using an undeclared variable deserves breaking code. horrible design failure. If someone has the nerve to put `window.undefined = true` in his code I think we should go to his house and teach him a lesson. – Raynos Feb 13 '11 at 16:26
  • 1
    @Fantius that checks for `null` & `undefined`. Most of the time you don't find `null` in the [wild](http://stackoverflow.com/questions/4748429/when-to-check-for-undefined-and-when-to-check-for-null). So it's pretty safe. – Raynos Feb 13 '11 at 16:27
  • I don't understand. Chrome is failing on both "if (someVar == null)" and "if (someVar != null)" for the undefined someVar. – Fantius Feb 13 '11 at 16:35
  • @Fantius: Has `someVar` been declared before you're using it in the `if()`? Using `typeof` obscures the fact that it hasn't been declared. What errors do you see in the console? – user113716 Feb 13 '11 at 16:36
  • No, it's undeclared. I'm testing to see if it was declared in a module which may or may not be loaded. – Fantius Feb 13 '11 at 16:40
  • 1
    @Fantius then check whether it exists in global scope i.e. `if (window.someVar === undefined)` – Raynos Feb 13 '11 at 16:43
1
(function(params, undefined) {
    // ...
    if (someVar === undefined) {
        ...
    }
    ...
    window.SomethingGlobal = SomethingGlobal;
}(params));

You can set declare undefined as a variable in your function. If that paramater is not passed in then you can garantuee it has the value of undefined.

It is always best to use closures like this to create a unique scope. If you need to hoist anything to global scope set it on the window manually.

Alternatively this will work:

if (someVar = void 0) {
   ...
}

void is a funny command. It expects an expression, It runs the expression and always returns undefined rather then the return value of the expression.

Raynos
  • 166,823
  • 56
  • 351
  • 396
0

As long as the 'big varity' of typeof includes such 'genius'*ionically* thing like object you may consider about this cheat:

typeof someVar >= 'u'ndefine

that will quicken stringcompare but decreases readability and cleanness of your code.

However short is beautify and maybe it's good to remind by that there is such a thing like the >=and =<-operator for strings that is often forgotten and so not used. ;)

Nadu
  • 2,401
  • 1
  • 25
  • 30
0

There's no real way to make that more concise. Since that code tests if the variable someVar is declared, the only safe use of the name someVar is as the operand to typeof (all other uses will raise an error if the variable is not declared).

I guess you could make the rest of the expression somewhat shorter by using a function:

function isUndefined(type)
{
    return type === 'undefined';
}

if (isUndefined(typeof someVar)) {
    // ...
}

But that's probably not worth the trouble.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479