Recap

Grade 6, page 11 of 11. A one-minute read.

  • A function is a named, reusable block of code. Functions help you avoid repeating yourself (DRY).
  • Declare a function with function name() { ... }. Call it with name().
  • 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.
  • return sends a value back to the caller and ends the function immediately. No return means the function gives back undefined.
  • console.log shows a value to you. return gives 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.

Start learning