Week 4 Day 1 Js Lesson:
Hey, campers👋
I hope your fingers are feeling strong from all that typing and your brains are warmed up because today we’re officially stepping into
Advanced JavaScript territory.
You’ve already met functions before, like old friends — but today, we’re going to dig deeper, learn their secrets, and make them work harder for us.
We’ll cover:
✅
Function Declarations vs. Function ✅Expressions
✅
Arrow Functions in Depth
✅
Default Parameters
✅
Rest & Spread Parameters
1️⃣ Function Declarations vs. Function Expressions
In JavaScript, there’s more than one way to create a function.
Function Declaration
This is the classic way — you just declare it, and JavaScript “hoists” it to the top of its scope, meaning you can call it even before it’s written in your code.
// Function Declaration
function sayHello() {
console.log("Hello, campers!"); }
sayHello(); // ✅ Works even if we call it before the function is defined
Key points:
➥Hoisted (can be used before they appear in code)
➥Good for situations where order in code doesn’t matter
Function Expression
Here, we store the function inside a variable.
These are
NOT hoisted, so you can only use them
after they are defined.
// Function Expression
const sayHi = function() {
console.log("Hi, campers!"); };
sayHi(); // ✅ Works here
If we tried calling sayHi() before its definition — ❌ error.
Why use them?
➥More flexible (can pass them around like normal variables)
➥Useful in callbacks and dynamic code
2️⃣ Arrow Functions — in Depth
We’ve met them before, but let’s explore their deeper behavior.
// Normal function
const add = function(a, b) {
return a + b; };
// Arrow function
const addArrow = (a, b) => a + b;
Differences:
➥
Shorter Syntax — Good for quick, one-line functions.
➥
this Behavior — Arrow functions don’t have their own this.
Instead, they use the this from the surrounding scope.
Example:
const person = { name: "Meresa", normalFunc: function() {
console.log("Normal:", this.name); }, arrowFunc: () => {
console.log("Arrow:", this.name); } };
person.normalFunc();
// Normal: Meresa person.arrowFunc();
// Arrow: undefined (because arrow uses global this)
Best use for arrow functions:
➥Small callbacks
➥When you want to keep this from the surrounding scope
3️⃣ Default Parameters
Sometimes, you want your function to have a “backup value” if no argument is given.
function greet(name = "Camper") {
console.log(Hello, ${name}!); }
greet("Abebe"); // Hello, Abebe!
greet(); // Hello, Camper!
Why useful?
➥Avoids undefined when arguments are missing
➥Makes functions safer and easier to use
4️⃣ Rest & Spread Parameters
Rest Parameters (...)
When you don’t know how many arguments will be passed, rest parameters collect them into an array.
function sumAll(...numbers) {
let sum = 0;
for (let num of numbers) {
sum += num; }
console.log(sum); }
sumAll(1, 2, 3); // 6
sumAll(5, 10, 15, 20); // 50
Spread Operator (...)
Instead of collecting arguments, spread takes an array and
spreads its values into separate arguments.
const nums = [1, 2, 3];
console.log(Math.max(...nums)); // 3
Uses of spread:
➥Copy arrays:
const arr1 = [1, 2, 3];
const arr2 = [...arr1];
➥Merge arrays:
const arr3 = [4, 5];
const merged = [...arr1, ...arr3]; // [1,2,3,4,5]
💡
Summary of Today’s Power-Ups:
✅Declarations vs. Expressions — know when to use each
✅Arrow functions — short & keep this
✅Default parameters — safer function calls
✅Rest & spread — handle many values with elegance