🧠 10-Day Challenge: Day 5 Functions & Scope in JS


#webdev #javascript #programming #learning
Welcome to Day 5 of the challenge!4
Today, we’re unlocking the core building blocks of JavaScript: Functions and Scope. Functions help you organize, reuse, and simplify your code — and understanding scope gives you control over your variables.
🔧 What Is a Function?
A function is a reusable block of code that performs a specific task.
You define it once and use it wherever you need it.
Think of a function like a vending machine:
Insert input → Process → Get output
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("Smriti")); // Hello, Smriti!
✅ Function Expression
const greet = function(name) {
return "Hi, " + name + "!";
};
Function expressions are not hoisted, so you can't call them before they’re defined.
⚡ Arrow Function (ES6+)
Arrow functions provide a shorter syntax and behave differently with this.
const greet = (name) => {
return "Hey, " + name + "!";
};
If the function has only one expression, you can make it even shorter:
const greet = name => "Hey, " + name + "!";
🔄 Parameters vs Arguments
Parameters: Variables listed in the function definitionfunctio
n greet(n
ame)
→ name
is a parameter
Arguments: Actual values passed when calling the functiongreet("
Smri
ti")
→ "Smr
i
ti"
is an argument
🔁 The return Keyword
The return keyword is used to output a value from a function.
function add(a, b) {
return a + b;
}
console.log(add(3, 4)); // 7
If no return, the function returns undefined.
📦 Scope in JavaScript
✅ Function Scope
Variables declared with var are function-scoped:
function showMessage() {
var message = "Hello";
console.log(message);
}
// message is not accessible outside
✅ Block Scope
let and const are block-scoped:
if (true) {
let msg = "Inside block";
const x = 42;
}
// msg and x are not accessible here
🔄 Normal Function vs Arrow Function: What's the Difference?
✅ Mini Task: Factorial Function
💡 Factorial of 5 \= 5 × 4 × 3 × 2 × 1 = 120
💻 Solution:
function factorial(n) {
let result = 1;
for (let i = 1; i <= n; i++) {
result *= i;
}
return result;
}
console.log(factorial(5)); // 120
Try writing the same using a recursive function!
❓ Interview Questions (Day 5 Topics)
What’s the difference between a function declaration and a function expression?
What does the return keyword do in a function?
What is the difference between function scope and block scope?
What are the key differences between arrow functions and regular functions?
�� That’s a Wrap for Day 5!
Now you understand how to create and use functions, pass and return values, and manage variable visibility through scope.
Functions = power + reusability.
👉 Coming up in Day 6: Arrays & Array Methods—where we’ll learn how to store and work with lists of data.
Keep coding, and share your factorial function below, which you wrote with a different logic! 💪💻
Subscribe to my newsletter
Read articles from Smriti Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Smriti Singh
Smriti Singh
👩💻 Frontend Developer | Learning Full-Stack | Exploring Web3 I enjoy building easy-to-use websites and apps with a focus on clean UI/UX. Currently expanding into full-stack development to grow my skillset. I’ve worked on exciting Web3 projects and love exploring how blockchain can shape the future of the web.