Functions: Your Own Custom Commands

Write it once, use it a thousand times. Eleven short pages from Grade 6 of Middle School, about 16 minutes of reading in all.

What you’ll learn

  • What functions are and why they matter
  • Declaring and calling functions
  • Parameters and arguments
  • Returning values (and why return isn't console.log)
  • Default parameters
  • Function expressions and arrow functions
  • Functions that use other functions
  • Habits for writing great functions

The pages

Eleven pages, about 16 minutes.

  1. What Is a Function? Let's say you've written some code that greets someone in a fancy way. Lovely. Now you need that fancy greeting in…
  2. Declaring and Calling a Function Here's how you make a function. Let's break it down. function is the keyword that says "I'm making a function."…
  3. Parameters and Arguments Our banner always says "Welcome, friend!" That's a bit impersonal. What if we want to welcome people by name? We need…
  4. Return Values So far our functions have printed things. But most of the time, you want a function to calculate something and give…
  5. Default Parameters Sometimes you want a parameter to have a sensible value if the caller doesn't provide one. You can give it a default…
  6. Function Expressions Here's a mind-bending idea: in JavaScript, a function is a value, just like a number or a string. That means you can…
  7. Arrow Functions In 2015, JavaScript introduced a shorter way to write function expressions, called arrow functions. They're called…
  8. Functions Working Together Functions really shine when you combine them. Big problems become a collection of small, easy problems, each solved by…
  9. Good Function Habits Writing a function is easy. Writing a good function takes a bit of practice. Here are the habits that will make your…
  10. Mini Project: The Converter Toolkit Let's build a little toolkit of useful conversion functions, and then use a loop to print a handy conversion table…
  11. Recap The key ideas of Grade 6, JavaScript functions, on one page. A function is a named, reusable block of code. Functions…

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