Functions can be used just like any other value: they can be passed around, assigned to variables, or they can be stored in arrays or objects.
const introduction = () => "Hello! My name is "
const welcome = (name, intro) => intro() + name
welcome("Kurt", introduction()) // Hello! My name is Kurt
- Take other functions as arguments, return other functions
- Common example:
Array.map
function, take a function and applies it to every element in an array:
const numbers = [1,2,3,4,5]
const transformNums = x => x + 2
numbers.map(transformNums) // [3,4,5,6,7]
another example:
const makeGreeter = greeting => name => `${greeting}, $[name}`;
const greet = makeGreeter('Hello')
greet('Kurt') // Hello, Kurt
- current values of all the variables, objects, network sockets, etc.
- It is basically all of the information that represents what is currently happening in the application.
- pure functions can’t access or mutate shared state.