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?

@mschot
Copy link

mschot commented Sep 6, 2013

I think it has more to do with initialisation then whether the variable is 'defined' or undefined
Javascript dynamically initialises properties (unlike a lot of other languages) and sets the property to undefined when the property is called.

Consider the following:

<script>
    var testVar = undefined;

    var testObj = {
        test : undefined
    }

    //initialised but set to undefined
    if (!testVar) {
       alert('no error for test variable');
    }
    //initialised but set to undefined
    if (!testObj.test) {
        alert('no error for test property');
    }
    //not initialised property
    if (!testObj.test2) {
        alert('no error for test property 2');
    }

    //not initialised variable
    if(!testVar2) {
        alert( alert('no error for test variable 2'));
    }
</script>

although testVar === undefined it does not throw a referenceError as it is initialised, but testVar2 does throw an Error as it is not initialised.

if( window.schrodinger) will function if shrodinger is defined but is FALSE or 0. I use typeof these day.

@davidhund
Copy link
Author

"JS dynamically initialises properties" – that's what I thought although I do not yet quite understand it.

Anyway, most people use typeof which seems the way to go. It's just that I'm trying to understand how window.foo works..

B.t.w.: if(window.schrodinger) alert('hello') does not work when [window.]schrodinger == FALSE or 0. So it still seems valid to use window.var.

I'm probably thick but I still do not understand why window.var does not throw a reference error... :)

@mschot
Copy link

mschot commented Sep 7, 2013

Doh... I meant if(!window.schrodinger) instead of if(window.schrodinger).
What I meant with this is that
if( typeof(schrodinger) !== "undefined"){ ... }
can be replaced by
if( window.schrodinger !== "undefined"){ ... }
but that is of course is not the same as
if(!window.schrodinger){ ... }

Regarding the reference error: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError
States "A ReferenceError is thrown when trying to dereference a variable that has not been declared."

Which as far as I understand ( http://stackoverflow.com/questions/4955198/what-does-dereferencing-a-pointer-mean ) means that it throws and error when the data can not be found in the memory.

When a property of an object is called it does dynamically create a reference from the property (but not the variable) to a memory location and sets that memory location with undefinded.

So after it is (dynamically) created it can find something in the memory at that location and therefore will not throw a referenceError.

@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