Skip to content

Instantly share code, notes, and snippets.

@shanemhansen
Created May 29, 2015 17:58
Show Gist options
  • Save shanemhansen/0a0553c8dd49c967fa02 to your computer and use it in GitHub Desktop.
Save shanemhansen/0a0553c8dd49c967fa02 to your computer and use it in GitHub Desktop.

Javascript surprises.

Q: What happens when I run:

console.log(dne);

A: Reference Error because dne does not exist.

Q: How about:

console.log(window.dne);

A: Undefined, even though all properties on the host object (window) the same as global variables

Q: How about:

(function() {
console.log(dne);
var dne ="they do exist!";
})()

A: Undefined, the presence of var anywhere in the function declares but does not initialize the variable.

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