Javascript functions are created by using the keyword function followed by a descriptive function name and opening and closing parentheses ( ), followed by lines of code wrapped by curly braces., like so:
function (parameter1, parameter2) {
//this function does nothing
//lines of code would go in here
//this block is called the body of the function
}
Values can be passed to a function when parameters are specified inside the function parentheses when the function is called. Javascript is flexible in the way it handles too many or too few arguments passed when a function is called. If too many arguments are passed, the extra ones are ignored. Missing arguments are assigned as undefined.
Bindings work within a scope or area of a program. Variables declared outside of a function or block have a global scope, meaning they are recognized throughout the whole program. Variables declared inside a function or block are only recognized in the area they were declared in and canât be seen globally, so they are termed local bindings. The keywords var and let are both used to define bindings but the scope of var is broader than let. If let is used to define a variable in a block thatâs inside a function, that variable will only be recognized inside the block and not in the whole function. If the same variable was instead defined using var, then it would be seen by the whole function and not just inside the block.
A pure function is like a math function that produces the same output every time itâs called with a given set of arguments. A (pure) math function would be the addition of two numbers. Every time the same numbers are added together the result is the same and thatâs all there is to it. Pure functions donât produce side effects. A side effect is like printing information to a screen or some other user input/output. A pure function cannot rely on side effects of other functions for input, like using the current time or generating random numbers as both would change each time the function would be run. Random values could be passed to a pure function, but couldnât be generated inside a pure function.