Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"To Be or Not to Be" about variables in JavaScript

Just doing a little introduction to JavaScript. I'm used to more than often testing the existence of my pointers in C++ so as not to crash.

Never did I read Hamlet, but I read this page about null and undefined in JavaScript.

And in a Nutshell I can:

if (varname == null)
if (varname === null)
if (typeof(varname) != 'undefined') 
if (varname != undefined)
if ('varname' in object) 
if (object.hasOwnProperty('varname')) 

Honestly that is a little too much for me :). What is the classical way in JavaScript for testing variables so as to avoid crashes?

like image 475
Stephane Rolland Avatar asked Feb 24 '11 14:02

Stephane Rolland


3 Answers

Because of errors thrown reading undeclared globals, checking a variable is best done using the third example, the typeof example.

if (varname == null)

will tell you whether the value is defined and nullish and throw an error if undeclared.

if (varname === null)

will tell you if the value is defined and exactly null and throw an error if undeclared.

if (typeof(varname) != 'undefined')

will tell you if the variable is defined or not without throwing an error.

if (varname != undefined)

is the opposite of the first.

if ('varname' in object)

will tell you if the object has a property either in itself or somewhere along its prototype chain. This is not guaranteed to work for host objects.

if (object.hasOwnProperty('varname'))

will tell you if the object has an own property, ignoring the prototype chain. This will break if a property named 'hasOwnProperty' has been set.

if (Object.hasOwnProperty.call(object, 'varname'))

is a more reliable version of the last.

like image 153
Mike Samuel Avatar answered Nov 09 '22 07:11

Mike Samuel


the usual way is to use the truthyness of values.

if(varname){

}

This covers most of the examples you gave

like image 25
Jamiec Avatar answered Nov 09 '22 07:11

Jamiec


The original question was this: What is the classical way in JavaScript for testing variables so as to avoid crashes?

This may be the classical way, and it's certainly a very practical way:

First, you should never, ever have undeclared variables. Use JSLint to be sure that you don't.

If you believe a is numeric, if (a != null) will tell you if there's some value in it (though possibly NaN), and if (a) will tell you if there's some nonzero value in it.

If you believe a is a string, if (a != null) will tell you if there's some value in it (though possible the empty string), and if (a) will tell you if there's at least one character in the string.

If you believe a is an object, if (a) will tell you if it's defined. Corollary: if (a && a.prop==7) is a safe way to check the value of a property.

If you have no idea what a is supposed to be, then you can still safely test it with if (a), though I can't say for sure how useful the results will be.

(Then again, if you have no idea what a is supposed to be, you have a much bigger problem than can be addressed here.)

like image 2
awm Avatar answered Nov 09 '22 06:11

awm