JS Sketchbooksee JavaScript think ✏️
← back to phase 3

3.2 — Parameters vs arguments

One input slot was nice — but real machines usually need several. Picture a café announcer: when an order is ready it shouts the customer’s name and their drink — “Priya, your chai is ready!”. Two pieces of information, two input slots.

And with two slots comes a brand-new way to fail. Hand the machine the same two values in the wrong order and it happily shouts “chai, your Priya is ready!” — no error, no warning, just confident nonsense. Why? Because the machine matches values to slots by position, and position only. That rule — and the two words everyone mixes up, parameter and argument — is today’s whole lesson.

watch it happen
function callOut(customer, drink) {
  console.log(customer + ", your " + drink + " is ready!");
}

callOut("Priya", "chai");
callOut("chai", "Priya");
callOut("Aisha");

The engine reads the definition: a machine named callOut with TWO input slots, customer and drink — in that order. Nothing runs, and the slots don’t even exist yet: they get built fresh at every call, and demolished when the call ends.

callOutcustomeremptydrinkemptyslot 1 · slot 2 — filled in that order, always
console(nothing logged yet)
under the hood

The deeper story, with the real names for things — this part is what turns “I saw it” into “I can explain it.”

The two words everyone mixes up, now yours for good: a parameter is a slot’s name, written once in the definition — customer, drink. An argument is the actual value you drop in at one specific call — "Priya", "chai". Parameters live in the blueprint; arguments arrive at GO time, and they’re matched purely by position: first argument → first parameter, second → second.

Too few arguments? No error — the leftover slot holds undefined, the same “never set” value from lesson 1.7, and the body runs with it.

Too many? The extras are quietly ignored. Both are famous sources of sneaky bugs, because nothing crashes — the program just produces confident nonsense, which is exactly what automation testers get paid to catch.

One more thing you saw without noticing: every call gets brand-new, empty slots — Priya’s values didn’t linger into the next call. Slots are born at the call and destroyed the moment it finishes. Hold onto that fact: in lesson 3.7 you’ll meet the spectacular exception — a function that walks away still carrying its slots.

your turn

⌨️ two slots, one trap

A weather announcer with two parameters — where a swapped argument produces obvious nonsense (“It is Chennai degrees in 30”). Build it so the order comes out right, twice.

requirements:

  • A function named weather with TWO parameters: city first, then temp (a number).
  • Each call prints It is <temp> degrees in <city> — note the twist: the parameters arrive city-first, but the sentence says temp-first. You must place each slot where the sentence needs it, not where it arrived.
  • Two reports: Chennai at 30, then Oslo at 3 — numbers without quotes.

when you press RUN, the console must show exactly:

It is 30 degrees in Chennai
It is 3 degrees in Oslo

⌨️ a machine that decides

Build a greeting machine with a brain: given the hour of the day, it picks the right greeting by itself. Functions meet Phase 2’s if/else.

requirements:

  • A function named greetByTime with one input slot: hour (a number, 0–23).
  • For hours before 12 it prints exactly Good morning! — for every other hour, exactly Good evening!.
  • Prove it works by calling it for hour 9 and for hour 20 — matching the expected output below.

when you press RUN, the console must show exactly:

Good morning!
Good evening!

✏️ Quick check 1

No options — type exactly what the console shows:

function tag(animal, sound) {
  console.log(animal + " says " + sound);
}

tag("moo", "cow");

✏️ Quick check 2

callOut("Aisha") — two slots, one value. Type what the drink slot holds while the body runs:

✏️ Quick check 3

In the call callOut("Priya", "chai") — is "chai" a parameter or an argument? Type the word.

teach it back

🗣️ Now teach it back

A friend asks: “parameter, argument — same thing, right?” Set them straight: explain the difference, and why callOut("chai", "Priya") comes out wrong even though both values are right there.

Write it as if your friend is sitting next to you. Saved to your journal — future-you will use these notes to teach others.

a few sentences, minimum — you’ve got this
to remember
Parameter = the slot’s NAME in the definition. Argument = the VALUE you drop in at one call.
Matching is pure position: first argument → first slot. The machine never guesses meaning.
A slot no value arrived in holds undefined — no error, just confident nonsense in the output.
Every call gets fresh empty slots, wiped after. (The spectacular exception lives in 3.7…)
next: 3.3 return →