tags:

views:

186

answers:

5

Hi

I want to make an if statement that if the value is not defined then let it go through.

I tried

if (something != "undefined")

and

if (something !== "undefined")

However it seems to go into the if statement no matter what. If it is undefined it goes through. If it is defined it goes through. Am I doing something wrong?

+2  A: 

You need to use typeof.

if( typeof something != "undefined" )

Jacob Relkin
Or just `something !== undefined`, assuming you've already done `var undefined`, pre-cautiously.
J-P
Good to see you added the quotes now. However, as mentioned in [my answer](http://stackoverflow.com/questions/2985771/how-to-check-for-undefined-in-javascript/2985784#2985784), note that strict comparison (`!==`) is not necessary in this case, since `typeof` will always return a string.
Mathias Bynens
Mathias: using strict or non-strict comparison here is a matter of personal taste. Both will always work, and neither is more correct. It could depend on whether your default position is to always use strict comparison unless specifically requiring type coercion (as recommended by Crockford, for example) or whether you prefer to use non-strict comparison except when strictness is required.
Tim Down
+2  A: 

Yes: you're comparing against a string rather than the undefined property of the global object. Instead, do

if (something !== undefined) {
   ...
}

... or even better:

if (typeof something != "undefined") {
    ...
}
Tim Down
You have to assign the `undefined` property globally before the first solution works, haven't you? (At least in Firefox I can't find a default one set; Chrome has `window.undefined` set to `"undefined"`)
Marcel Korpel
Marcel: `undefined` is specified in the ECMAScript 3 spec as a property of the global object, so is by definition global. This is implemented in all major browsers, including Firefox (the last one not to implement it was IE 5). As a property of the global object rather than a literal (as `null` is), `undefined` can be redefined, which is what makes the second solution superior.
Tim Down
Just tested this using `console.log`, you're right. It just doesn't appear in Firebug as property of `window`.
Marcel Korpel
As a property of the global object, it has the `DontEnum` attribute, meaning it won't show up in `for...in` loops (which is what I'd imagine Firebug uses). Its existence can be shown by the expression `"undefined" in window`, which returns `true`.
Tim Down
+3  A: 

If it is undefined, it will not be equal to a string that contains the characters "undefined", as the string is not undefined.

You can check the type of the variable:

if (typeof(something) != "undefined") ...

Sometimes you don't even have to check the type. If the value of the variable can't evaluate to false when it's set (for example if it's a function), then you can just evalue the variable. Example:

if (something) {
  something(param);
}
Guffa
No need for the parentheses: `typeof` is an operator, not a function.
Tim Down
@Tim - It can be used both ways.
Nick Craver
@Tim: @Nick is correct. See https://developer.mozilla.org/en/Core_Javascript_1.5_Reference/Operators/Special_Operators/typeof_Operator
Mathias Bynens
Yes, I know that it *works* with the parentheses, which is because the parentheses here form the grouping operator that simply evaluates and returns the operand inside. I merely said that they were unnecessary.
Tim Down
+3  A: 
if (typeof foo == 'undefined') {
 // Do something
};

Note that strict comparison (!==) is not necessary in this case, since typeof will always return a string.

Mathias Bynens
What's with the semi-colon (`};`)?
J-P
@J-P: The semicolon after the closing brace is just an empty statement.
Gumbo
@Gumbo, sorry, what I meant to ask was: "What purpose is the semi-colon serving?"
J-P
@J-P That’s just a personal preference. I like to add optional semicolons — the `if` block can be seen/rewritten as one line of code, and then it makes sense to append the semicolon, because that’s how I end pretty much every other statement. `if (typeof foo == 'undefined') { };` Also, this ensures compatibility with some JavaScript minifiers. I’m aware JSLint advises against this, but I just don’t see the point — these semicolons are harmless and if anything, enforce a slightly stricter coding style.
Mathias Bynens
I've not encountered a minifier that can't handle `if(){}` without a `;` ... Which minifiers are you referring to? You say that this is how you end every other statement... I guess that's true. But, a block statement `{}` is already a statement in and of its own. Adding a `;` makes it two statements, technically. Syntactically, it's redundant. Even automatic semi-colon insertion won't add a semi-colon there...
J-P
@J-P: I guess I started doing it years ago after reading [the Packer documentation](http://dean.edwards.name/packer/usage/sample.html). Packer expects semicolons after `function() {}` declarations. You’re right though — apparently it’s not required after `if` statements, but somehow I still think it makes sense.
Mathias Bynens
A: 

Provocant question: Why do you want to check if something undefined? Dont try to check every income like java-boys "if (str!=null && str ...)"

Read the "javascript all about types" tutorial from Mathias Reuter

Christian Harms