JavaScript null check -


i've come across following code:

function (data) {     if (data != null && data !== undefined) {         // code here     } } 

i'm new javascript, but, other questions i've been reading here, i'm under impression code not make sense.


in particular, this answer states

you'll error if access undefined variable in context other typeof.

update: (quote of the) answer above may misleading. should «an undeclared variable», instead of «an undefined variable».

as found out, in answers ryan ♦, maerics, , nwellnhof, when no arguments provided function, variables arguments declared. fact proves wrong first item in list below.


from understanding, following scenarios may experienced:

  • the function called no arguments, making data undefined variable, , raising error on data != null.

  • the function called null (or undefined), argument, in case data != null protects inner code, rendering && data !== undefined useless.

  • the function called non-null argument, in case trivially pass both data != null and data !== undefined.

q: understanding correct?


i've tried following, in firefox's console:

-- [15:31:31.057] false != null [15:31:31.061] true -- [15:31:37.985] false !== undefined [15:31:37.989] true -- [15:32:59.934] null != null [15:32:59.937] false -- [15:33:05.221] undefined != null [15:33:05.225] false -- [15:35:12.231] "" != null [15:35:12.235] true -- [15:35:19.214] "" !== undefined [15:35:19.218] true 

i can't figure out case data !== undefined after data != null might of use.

an “undefined variable” different value undefined.

an undefined variable:

var a; alert(b); // referenceerror: b not defined 

a variable value undefined:

var a; alert(a); // alerts “undefined” 

when function takes argument, argument declared if value undefined, , there won’t error. right != null followed !== undefined being useless, though.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -