JavaScript Roadmap — Day 29: Return Values and Scope — Getting Results Back
Return Values and Scope — Getting Results Back
Functions can send data back to the code that called them using the return statement. And scope determines where variables are visible. Master these and you'll write functions that are truly useful and bug-free.
1. The Return Statement — Sending Data Back
The return statement is how a function sends a value back to the code that called it. Without return, a function returns undefined by default — which means you can't use its result in calculations or store it in variables.
Think of a function like a vending machine. You put money in (arguments), press a button (call the function), and the machine returns a snack. Without the return, the machine would just make noise but give you nothing! The return statement also immediately exits the function — no code after it will run.
💡 The key insight: return does two things: (1) it sends a value back to the caller, and (2) it immediately stops the function. Any code after return will never execute. This makes return perfect for early exits when validation fails.
Question 1: What does a function return if there is no return statement?
2. Understanding Scope — Where Variables Live
Scope determines where variables are accessible in your code. Think of scope like different rooms in a house. Variables declared in a room (function) can't be seen from outside that room. But variables declared in the living room (global scope) can be seen from everywhere.
JavaScript has three main types of scope: global scope (accessible everywhere), function scope (accessible only inside the function), and block scope (accessible only inside {} blocks). Understanding scope prevents naming conflicts and unexpected bugs.
3. Global vs Local Scope — The Difference
Global variables are declared outside any function and can be accessed from anywhere. Local variables are declared inside a function and only exist while that function runs. This is crucial for avoiding naming conflicts — you can use the same variable name in different functions without them interfering.
A key principle: functions can read global variables, but when you assign to a variable inside a function, JavaScript creates a local variable unless you explicitly tell it otherwise. This prevents accidental modification of global variables.
Question 2: What happens when you declare a variable inside a function without let, const, or var?
4. Block Scope — {} Creates Boundaries
With let and const, any pair of curly braces {} creates a new scope. This includes if statements, for loops, while loops, and standalone blocks. This is different from var, which ignores block scope and only respects function scope.
Block scope is why modern JavaScript prefers let and const over var. Variables declared with let inside a block cannot be accessed outside that block, preventing accidental leaks and making code more predictable.
Question 3: Which keyword creates block scope?
Question 4: What will be the output?
let x = 10;
function test() {
let x = 20;
console.log(x);
}
test();
console.log(x);
5. Common Mistakes to Avoid
6. Best Practices — Professional Patterns
return. Don't just log it. This makes your functions reusable and testable.var ignores block scope and can cause bugs. Always use let (for changing values) or const (for constants).return early. This reduces nesting and makes code clearer.7. Try It Yourself — Scope Playground
See how scope works in real time. Click the buttons to test global vs local variables.
8. Practice Tasks
Task 1 — Easy: Return the Square
Write a function square(num) that returns the square of a number. Test with 5 (returns 25) and 10 (returns 100).
Task 2 — Medium: Local vs Global
Create a global variable counter = 0. Write a function increment() that creates a LOCAL variable also called counter and increments it. Show that the global counter remains unchanged.
Task 3 — Hard: Validation with Early Return
Write a function divide(a, b) that returns the division result. If b is 0, return the string "Error: Cannot divide by zero" immediately (early return). Test with (10, 2) and (10, 0).
Question 5: What is the output of the following code?
let x = 5;
function example() {
let x = 10;
return x;
}
console.log(example());
console.log(x);