Skip to content

Instantly share code, notes, and snippets.

@natevenn
Created May 4, 2016 17:01
Show Gist options
  • Select an option

  • Save natevenn/f98231601435ffc3b8c9cf046d4e0601 to your computer and use it in GitHub Desktop.

Select an option

Save natevenn/f98231601435ffc3b8c9cf046d4e0601 to your computer and use it in GitHub Desktop.
To declare a variable use var something = or var something;
Variables in JavaScript are lexically scoped, if nested then it is available in all those scopes
a var inside a scope takes precedence over the var with same name outside of scope, also called blocked scope
a workaround of blocked scopes is to introduce a new scope in a then block. Also called an IIFE or immediately invoked function expression
An IIFE enables you to attach private data to a function and therefore dont have to declare a global variable
A globally scoped variable is avaiable to entire program
two disadvantages of gloabal var are the side effects - they are less robust, behave less predictably, and are less reusable
and all JS on a webpage share the same global variables so naming can clash with other var.
to avoid this JS has module systems dont interface with global scope
A closure is a function plus the connection to the scope in which the function was created.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment