Functions are the single most important building block in JavaScript. Almost everything you write from here on will live inside one. This lesson is a map of where the Functions sections are headed, so each new idea has a place to land. Learn them in order: first the idea, then how to make them, then how to use them.
A function is a named, reusable block of code designed to do one particular task. You write it once, then run it whenever you need it, as many times as you like, with different inputs each time.
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("Ada"));
console.log(greet("Grace"));
calculateTax(price) explains itself far better than the raw mathEvery time you wrote console.log(), Math.random(), Number("42"), or "hi".toUpperCase(), you called a function someone else wrote. In JS Control Flow you even saw a preview of writing your own. Now you will learn to write them properly.
function add(a, b) { // DECLARING the function; a and b are PARAMETERS
return a + b; // the BODY; return sends back the RESULT
}
let sum = add(2, 3); // CALLING it; 2 and 3 are ARGUMENTS
| Term | Meaning | In the example |
|---|---|---|
| Parameters | The placeholder names in the definition | a, b |
| Arguments | The real values passed in when calling | 2, 3 |
| Body | The work done inside the braces | return a + b; |
| Return value | What comes back out | 5 |
A recipe card (the function) lists ingredients (parameters) and steps (the body). Writing the card does not make dinner. Cooking it (calling the function) with real ingredients (arguments) produces a meal (the return value). You can cook the same recipe many times with different ingredients.
The Functions sections build one idea at a time:
| Step | Lesson | Question it answers |
|---|---|---|
| 1 | Function Intro | How do I write one? |
| 2 | Function Invocation | How do I run it, and when does it run? |
| 3 | Function Parameters | How do I give it information? |
| 4 | Function Returns | How do I get an answer back? |
| 5 | Function Arguments | What if I pass more or fewer values? |
| 6 | Function Expressions | Can a function be stored in a variable? |
| 7 | Arrow Functions | Is there a shorter way to write them? |
| 8 | JS Timers | How do I run a function later, or repeatedly? |
Steps 1 to 4 are the beginner core; get those solid before moving on. Steps 5 to 8 build on them.
Functions go much deeper than this, and later sections return to them. So the names are familiar when you meet them, here is what the advanced path covers:
this keyword: how a function knows which object it belongs to (Objects section)thisIn function area(w, h) { return w * h; } followed by area(4, 5), what are 4 and 5?
w and h are the parameters, and 20 is the return value.You write a function declaration but never call it. What happens when the page loads?
Which of these is not a reason to use functions?
greet with three different namesadd example, point to the parameters, the arguments, the body, and the return value