tags:

views:

81

answers:

3

How to handle a not defined value in java script

if (oldins == ins)

oldins is not defined How i check???????

+4  A: 
if ((typeof(oldins) != "undefined") && (oldins == ins))
etc
+1 for being the right answer. Btw, no need for the parentheses around the `oldins` in the `typeof` check: `typeof` is an operator, not a function.
Tim Down
+3  A: 

Unset variables would evaluate to a value of 'undefined'. 'undefined' is a value type like null and NaN so it would be:

if ( typeof(oldins) == 'undefined' )

Edit: Fixed per comments. Leaving the answer since the comments are helpful, but there were more correct answers.

MacAnthony
Well `undefined` is not exactly like `null` or `NaN` - it's just an identifier and it can be reassigned!
Pointy
This will give you an error if `oldins` has never been declared. Use `typeof` instead.
Tim Down
Who wouldn't declare their variables??? ;)Fair comments
MacAnthony
@Pointy: Actually, `NaN` is also a variable as is `Infinity` - both can be reassigned. `null` is the only true keyword among these.
casablanca
@casablanca wow really? Well after I typed in that comment I was going to try those but I got distracted, so thanks!!
Pointy
+1  A: 
if (oldins !== undefined && oldins === ins) {

}
bjg
This will give you an error if `oldins` has never been declared. Use `typeof` instead.
Tim Down