typescript · 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 sits in the round brackets with a type label, like name: string in function greet(name: string). Inside the block, use it like any variable.
The value you drop in when you call is the . greet("Mia") puts "Mia" into name. Feed it the wrong type, like greet(7), and the checker stops you with TS2345.
function greet(name: string) {
console.log(`Hello, ${name}!`);
}
greet("Mia");
greet("Leo"); Separate two parameters with commas, and give every slot an argument, or the checker says TS2554.
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: string, drink: string) {
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 run it and watch one machine make two cards.
fix it
Rex is hungry, but the checker says no. Fix it!
- Read the error:
TS2345: Argument of type 'string' is not assignable to parameter of type 'number'. - The scoops slot is labelled
number, but "two" is in quotes, so it is a string. - Change "two" to 2, with no quotes, then press run.
nailed it
your functions take orders now. :D
- A parameter is a labelled slot in the function's brackets.
- An argument is the value you drop in when you call.
- Arguments fill parameters in order, and their types must match.
new words in your collection
- parameter
- argument
This lesson isn’t saved. A free account remembers every lesson you finish.