- A function is a named, reusable block of code. Functions help you avoid repeating yourself (DRY).
- Declare a function with
function name() { ... }. Call it withname(). - Parameters are placeholders in the definition. Arguments are the actual values you pass in. They're matched by position.
- Missing arguments become
undefined. Use default parameters (name = "friend") to avoid that. returnsends a value back to the caller and ends the function immediately. No return means the function gives backundefined.console.logshows a value to you.returngives a value to your program. They're not the same!- Functions are values. You can store them in variables (function expressions) and pass them to other functions (callbacks).
- Arrow functions:
(a, b) => a + b. Without curly braces, the value is returned automatically. - Good functions have verb names, do one thing, stay short, and prefer returning over printing.
Reading is step one
CurlyScript turns these notes into a game: short questions, an explanation after every answer, and a test at the end of each grade. Free, with nothing to sign up for.