Skip to content

Instantly share code, notes, and snippets.

@itiswicked
Last active December 1, 2015 14:32
Show Gist options
  • Save itiswicked/c1a6e7ea958bdb6d8367 to your computer and use it in GitHub Desktop.
Save itiswicked/c1a6e7ea958bdb6d8367 to your computer and use it in GitHub Desktop.

Javascript Basics & Functions

Functions

  • functions are defined with the keyword function
  • Values are returned with the keyword return
    • There are no implicit returns in javascript.
  • function defintions are expressions and objects, and are first class citizens
    • Functions can therefore be passed around like other pieces of data
    • Pass Functions as Arguments

Side Effects

  • Things like console.log() are side effects because they don't return anything.
  • Pure function:
    • One that has no side effects, nor depend or modify variables defined outside its current scope.
  • Impure functions are functions that have 1 or more of those things.

Scope

  • When in doubt define a varible with var. When you use var, you are declaring the var and assigning it
  • Defining a variable globally with or without var doesn't make a huge difference.
    • Not declaring a variable with var makes it global by default.
  • But when you define a variable inside a function, it makes a hunge difference.
    • var inside a function scopes the variable to the function only.
    • This means you can have multiple variable definitions with the same name in a different scope, as long as you declare with var
  • When reassigning a variable that is already declared, you do not need var

Function Declaration vs. expressions

  • Function declarations load before code gets executed.
  • Function expressions load when the JavaScript reaches that line of code.
  • You can call declared functions before they are defined, due to Hoisting.
  • Hoisting does not effect function expressions. The functions are defined ad hoc (as the code is run)

Variable Hoisting

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