JavaScript Curriculum
Don't Repeat Yourself
easyThe Nexus dashboard shows a price on 14 different screens. Right now \"$\" is hardcoded 14 times. When the product team says \"we're going international — support GBP too\", you'd have to edit 14 places and hope you don't miss one. There's a better way: write the formatting logic once, name it, and call it wherever you need it.
The copy-paste trap
You write "$" + price.toFixed(2) once. It works. Two weeks later you need it on another screen — paste. Then another — paste. Then the product team wants £ support. You search through your entire codebase trying to find every place you pasted it.
This is the copy-paste trap. The fix is a function.
A function is a named, reusable block of code. Write the logic once. Call it by name wherever you need it. Change it in one place — it updates everywhere.
The anatomy of a function
Every part of a function has a job. Click each highlighted section to see what it does:
← click any coloured part to learn what it does
Parameters and arguments
Parameters are the named placeholders you define in the function signature. Arguments are the actual values you pass in when calling it.
You can have zero parameters, one, or many — separated by commas.
How a function call actually works
Watch the flow: call site → function body → return value travels back:
1. call site
JavaScript sees the function call with your argument
2. body runs
The code inside the function executes
3. return
The return value travels back to the call site
Three things happen every time you call a function. First JavaScript sees the call and packages up your argument. Then it jumps into the function body and runs the code. Finally return sends a value back — and execution resumes exactly where it left off.
The return statement
return does two things: it sends a value back to the caller, and it exits the function immediately. Nothing after return runs.
A function without a return statement (or one that just says return) gives back undefined.
Arrow functions — the modern syntax
ES6 (2015) introduced a shorter syntax for functions. Today you'll see this everywhere in modern JavaScript codebases.
The classic syntax. Works everywhere. Hoisted — can be called before it is defined.
All four versions do exactly the same thing — double(5) returns 10 in every case.
The implicit-return one-liner is extremely common for small transformations — especially when you'll learn about array methods in the next lesson.
Default parameters
You can give parameters a fallback value that's used when the caller doesn't provide one:
This is much cleaner than checking if (!name) inside the function body.
Building your formatPrice function
Here's what the complete solution looks like — step by step:
Now if the product team adds JPY next month, you change exactly one function and every screen updates.
Challenge
Write a function called formatPrice(amount, currency) that returns a formatted price string. If currency is \"USD\" prefix with \"$\". If currency is \"GBP\" prefix with \"£\". If currency is \"EUR\" prefix with \"€\". Always show exactly 2 decimal places (use .toFixed(2)). Log formatPrice(9.5, \"USD\"), formatPrice(49, \"GBP\"), and formatPrice(100, \"EUR\").
Don't Repeat Yourself
easyThe Nexus dashboard shows a price on 14 different screens. Right now \"$\" is hardcoded 14 times. When the product team says \"we're going international — support GBP too\", you'd have to edit 14 places and hope you don't miss one. There's a better way: write the formatting logic once, name it, and call it wherever you need it.
The copy-paste trap
You write "$" + price.toFixed(2) once. It works. Two weeks later you need it on another screen — paste. Then another — paste. Then the product team wants £ support. You search through your entire codebase trying to find every place you pasted it.
This is the copy-paste trap. The fix is a function.
A function is a named, reusable block of code. Write the logic once. Call it by name wherever you need it. Change it in one place — it updates everywhere.
The anatomy of a function
Every part of a function has a job. Click each highlighted section to see what it does:
← click any coloured part to learn what it does
Parameters and arguments
Parameters are the named placeholders you define in the function signature. Arguments are the actual values you pass in when calling it.
You can have zero parameters, one, or many — separated by commas.
How a function call actually works
Watch the flow: call site → function body → return value travels back:
1. call site
JavaScript sees the function call with your argument
2. body runs
The code inside the function executes
3. return
The return value travels back to the call site
Three things happen every time you call a function. First JavaScript sees the call and packages up your argument. Then it jumps into the function body and runs the code. Finally return sends a value back — and execution resumes exactly where it left off.
The return statement
return does two things: it sends a value back to the caller, and it exits the function immediately. Nothing after return runs.
A function without a return statement (or one that just says return) gives back undefined.
Arrow functions — the modern syntax
ES6 (2015) introduced a shorter syntax for functions. Today you'll see this everywhere in modern JavaScript codebases.
The classic syntax. Works everywhere. Hoisted — can be called before it is defined.
All four versions do exactly the same thing — double(5) returns 10 in every case.
The implicit-return one-liner is extremely common for small transformations — especially when you'll learn about array methods in the next lesson.
Default parameters
You can give parameters a fallback value that's used when the caller doesn't provide one:
This is much cleaner than checking if (!name) inside the function body.
Building your formatPrice function
Here's what the complete solution looks like — step by step:
Now if the product team adds JPY next month, you change exactly one function and every screen updates.
Challenge
Write a function called formatPrice(amount, currency) that returns a formatted price string. If currency is \"USD\" prefix with \"$\". If currency is \"GBP\" prefix with \"£\". If currency is \"EUR\" prefix with \"€\". Always show exactly 2 decimal places (use .toFixed(2)). Log formatPrice(9.5, \"USD\"), formatPrice(49, \"GBP\"), and formatPrice(100, \"EUR\").