Skip to content

Instantly share code, notes, and snippets.

@davidhund
Created September 5, 2013 15:17
Show Gist options
  • Save davidhund/6451573 to your computer and use it in GitHub Desktop.
Save davidhund/6451573 to your computer and use it in GitHub Desktop.
Trying to figure out the differences (pros/cons) of checking variable existence

Considering the following scenario:

We need to use some var or function schrodinger but do not know if it is available.

So we try:

if(schrodinger){ ... } => ReferenceError: schrodinger is not defined

We could use.. if( typeof(schrodinger) !== "undefined"){ ... } => No error. Stuff does not run

BUT, we could also use.. if( window.schrodinger){ ... }

.. as (apparently) properties become undefined instead of null or something?

This last point I do not understand ("Why does window.schrodinger not throw an error?")

Is there any preference of using typeof(shrodinger).. over window.schrodinger?

@davidhund
Copy link
Author

Thanks Martijn, I'll read up on that info.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment