Skip to content

Instantly share code, notes, and snippets.

@alcedo
Last active December 21, 2015 01:59
Show Gist options
  • Save alcedo/6232023 to your computer and use it in GitHub Desktop.
Save alcedo/6232023 to your computer and use it in GitHub Desktop.
JavaScript Quirks and notes
/*********** Checking for not defined / undefined **********/
foo
// ReferenceError: foo is not defined
if(foo == null) console.log('this wont work');
// ReferenceError: foo is not defined
if (typeof foo == 'undefined' ) console.log('foo is undef');
// this is the only way to check for not defined
var foo; //define foo, but foo has no value
if (typeof foo == 'undefined' ) console.log('foo is undef');
//this works
if ( foo == undefined ) console.log('foo is undef');
// this works
null // used to indicate a deliberate non-value
undefined // used to indicate a value that hasn't been set yet
/////////////////
var test = someObject.methodTest;
test();
//Due to the first case, test now acts like a plain function call; therefore, this inside it will no longer refer to someObject. ie: LHS of test is window object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment