views:

160

answers:

3

In normal javascript, I can check either

if (i == undefined) or if (i === undefined)

or the "typeof"

however, within facebook, it will have the api_key append as the prefix in undefined.

i.e. it will become

if (a12345_i == a12345_undefined)

which is NOT undefined

so, what can I do to find out the variable is undefined or not?


Well. Thanks for all answers but I think I should emphasize one thing.. I have no problem to detect the null value in normal JS environment. The probem I face is when it is with Facebook as a FBML application.

A: 

Try this ...

if (i == null) or if (i === null)
if (i == '') or if (i === '')
pavun_cool
A: 

You can use x == null, which will check for null or undefined. However, the check you do should work. While a12345_undefined doesn't look like undefined, it is. For instance, this would work just as well:

var notDefined;
if (x === notDefined) { // ...

As long as you never assign anything into the variable undefined (which becomes a12345_undefined) you should be fine. undefined is just a normal variable in JavaScript, it just starts with undefined value. You could use any variable without a value as a comparison for undefined.

Hope that makes sense.

bcherry
+1  A: 

typeof(var) returns a string, you should be checking:

typeof(var) == 'undefined'
Janek