javascript · level 1 · unit 7 · lesson 2
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A is a slot where your function catches information.
Picture your function as a machine. Right now it does the same thing every time. Cut a slot in the front, and you can feed in something new on each go.
That slot is a parameter. It is a variable that lives in the round brackets of the function, like name in function greet(name). Inside the function, use it like any other variable.
The value you drop in when you call is the . Call greet("Mia"); and name holds "Mia". Call greet("Leo"); and the same machine greets Leo instead.
function greet(name) {
console.log(`Hello, ${name}!`);
}
greet("Mia");
greet("Leo"); Need two slots? Separate the parameters with commas, and the arguments too.
guess first
Two slots this time. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
function order(food, drink) {
console.log(`I would like ${food} with ${drink}.`);
}
order("lemonade", "chips");
your turn
Your turn. Run the birthday card machine.
Type a name between each pair of quotes and an age after each comma. Then press run and watch one machine make two cards.
fix it
Rex is hungry, but this crashes. Can you fix it?
- It is a ReferenceError. Which name has JavaScript never heard of?
- The parameter on line 1 is called pet, but line 2 uses a different name.
- Change animal to pet inside the curly brackets on line 2, then press run.
nailed it
your functions take orders now. :D
- A parameter is a slot in the function's round brackets.
- An argument is the value you drop in when you call.
- Arguments fill parameters in order: first into first.
new words in your collection
- parameter
- argument
This lesson isn’t saved. A free account remembers every lesson you finish.