JavaScript Foundations β’ StreetGeek Academy
Understand how to define, call, and reuse JavaScript functions, how parameters and return values work, and how scope affects where variables can be accessed.
A function is a reusable block of code that performs a specific task.
Instead of writing the same logic over and over, you define it once and call it whenever needed.
function greet() {
console.log("Welcome to StreetGeek!");
}
greet(); // Call the functionWelcome to StreetGeek!Functions can take parameters (placeholders) that receive arguments (real values).
function greetUser(name) {
console.log(`Hello, ${name}! Welcome back.`);
}
greetUser("Yogi");
greetUser("Alex");Hello, Yogi! Welcome back.Hello, Alex! Welcome back.You can use multiple parameters:
function showScore(user, score) {
console.log(`${user} scored ${score} points!`);
}
showScore("Yogi", 95);Functions can return data back to where they were called.
function add(a, b) {
return a + b;
}
let result = add(5, 3);
console.log(result); // 8return statement ends the function immediately.function getPortfolioStatus(projects) {
if (projects >= 10) return "Pro Developer π§ ";
if (projects >= 5) return "Rising Star π";
return "Beginner β‘";
}
let level = getPortfolioStatus(8);
console.log(level); // "Rising Star π"You can also store a function inside a variable:
const multiply = function (x, y) {
return x * y;
};
console.log(multiply(4, 5)); // 20A shorter syntax for writing functions.
const greet = (name) => {
return `Hello, ${name}!`;
};
console.log(greet("Yogi"));If the function is only one line, you can omit the return and {}:
const double = n => n * 2;
console.log(double(10)); // 20Scope determines where a variable is accessible in your code.
| Type | Created By | Accessible In |
|---|---|---|
| Global Scope | Declared outside any function | Everywhere |
| Function Scope | Inside a function (var) | That function only |
| Block Scope | Inside {} with let or const | That block only |
let message = "Global Message";
function showMessage() {
let message = "Local Message";
console.log(message);
}
showMessage(); // "Local Message"
console.log(message); // "Global Message"calculateTotal, not doThing)function greetUser(name, timeOfDay) {
console.log(`Good ${timeOfDay}, ${name}!`);
}
greetUser("Yogi", "morning");
greetUser("StreetGeek Student", "afternoon");<p id="portfolio-summary"></p>function getSummary(projects, followers) {
return `You have ${projects} projects and ${followers} followers.`;
}
const summaryText = document.getElementById("portfolio-summary");
summaryText.textContent = getSummary(8, 152);<p id="visitor-stats"></p>function getTotalVisitors(visits) {
let total = 0;
for (let i = 0; i < visits.length; i++) {
total += visits[i];
}
return total;
}
const weekVisits = [120, 90, 150, 200, 130, 80, 170];
const visitorStats = document.getElementById("visitor-stats");
visitorStats.textContent = `This weekβs visitors: ${getTotalVisitors(weekVisits)}`;<button id="dark-mode-btn">Toggle Dark Mode</button>const darkModeBtn = document.getElementById("dark-mode-btn");
const toggleDarkMode = () => {
document.body.classList.toggle("dark");
console.log("Dark mode toggled π");
};
darkModeBtn.addEventListener("click", toggleDarkMode);1) Whatβs the main purpose of a function?
2) Whatβs the difference between function add() {} and const add = () => {}?
3) What happens when a function reaches a return statement?
4) Whatβs the difference between global and local scope?
5) How would you pass data into a function?
Add a Reusable Stats Module to your portfolio homepage.
app.js:function getBadge(projects) {
if (projects >= 10) return "Gold π
";
if (projects >= 5) return "Silver π₯";
return "Bronze π₯";
}
function updateStats(name, projects) {
const stats = document.getElementById("stats");
const badge = getBadge(projects);
stats.textContent = `${name} has completed ${projects} projects β ${badge} Badge Earned!`;
}
updateStats("Yogi", 8);index.html:<p id="stats"></p>Next up β MODULE 5: Arrays & Objects (ES6 Essentials)
Youβll learn how to store and manage collections of data β and dynamically generate portfolio sections like your projects list straight from JavaScript.