- 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
- 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.
- When in doubt define a varible with
var
. When you usevar
, 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 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)