How to check for undefined or null variable in javascript
- by Thomas Wanner
We are frequently using the following code pattern in our javascript code
if(typeof(some_variable) != 'undefined' && some_variable != null)
{
// do something with some_variable
}
and I'm wondering whether there is a less verbose way of checking that has the same effect. According to some forums and literature saying simply
if(some_variable)
{
// do something with some_variable
}
should have the same effect. Unfortunately, Firebug evaluates such a statement as error on runtime when some_variable is undefined, whereas the first one is just fine for him. Is this only an (unwanted) behavior of Firebug or is there really some difference between those two ways ?