Organizational Research By

Surprising Reserch Topic

how to check for an undefined or null variable in javascript


how to check for an undefined or null variable in javascript  using -'javascript,null,undefined'

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
}


Is there a less verbose way of checking that has the same effect?

According to some forums and literature saying simply the following should have the same effect.

if (some_variable)
{
    // Do something with some_variable
}


Unfortunately, Firebug evaluates such a statement as error on runtime when some_variable is undefined, whereas the first one is just fine for it. Is this only an (unwanted) behavior of Firebug or is there really some difference between those two ways?
    

asked Oct 11, 2015 by loknath.ganji
0 votes
0 views



Related Hot Questions

10 Answers

0 votes
NULL
answered Oct 11, 2015 by ashishshukla
0 votes
NULL
answered Oct 11, 2015 by sameer rathore
0 votes
NULL
answered Oct 11, 2015 by vijayshukla80
0 votes
NULL
answered Oct 11, 2015 by badhwar.rohit
0 votes
NULL
answered Oct 11, 2015 by suyesh.lokhande
0 votes
NULL
answered Oct 11, 2015 by ankitarajoria4
0 votes
NULL
answered Oct 11, 2015 by dhananjayksharma
0 votes
NULL
answered Oct 11, 2015 by pradip.bhoge
0 votes
NULL
answered Oct 11, 2015 by amit.gupta
0 votes
NULL
answered Oct 11, 2015 by mannumits1

...