Functions begin with the keyword
function, followed by any
parameters (may have multiple or none) and then the
body with the statements the function executes. The traditional syntax always requires curly braces around the body (ES6 rules have exceptions). Two examples of different functions the book gives are below, showing some variety in parameters and complexity. Note that the first doesn’t return a value (would give
undefined) while the second does (using
- Parameters are used in the body like regular values, but are only supplied when the function is called.
- Each function has its own local scope when called. Variables declared inside a function can only be referenced inside the function, and not after. It’s basically its own little world, where it can reference global values if needed (not recommended) but nothing outside the function can reference it.
function keyword to set them up, instead using an
Arrow Function With Body
These are more similar to ES5 functions, since it gives you multiple lines and must implicitly return a value.
Arrow Function Without Body
Without the body, arrow functions are limited to one line and implicitly return what’s there. Great for simpler code so it’s easier to read.