How to check for undefined or null variable in javascript
Posted
by Thomas Wanner
on Stack Overflow
See other posts from Stack Overflow
or by Thomas Wanner
Published on 2010-04-01T09:17:18Z
Indexed on
2010/04/01
9:23 UTC
Read the original article
Hit count: 229
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 ?
© Stack Overflow or respective owner