Functions Lesson 17 of 30
JavaScript Scope and Arrow Functions
Understand where variables can be used (global, function and block scope), and write short functions with arrow syntax and callbacks.
Two ideas make functions much more powerful: scope, which decides where a variable can be used, and arrow functions, a shorter way of writing functions that you'll see in nearly all modern JavaScript.
What is scope?
Scope is the "area" of your code where a variable exists. Think of it like a classroom: what's written on the board in Room 101 can't be seen from Room 102.
Global scope
A variable created outside every function is global. Any code on the page can use it.
Function scope
A variable created inside a function exists only inside that function:
<pre id="out"></pre>
<script>
const school = "Mabini HS"; // global
function showRoom() {
const room = "Room 101"; // local to showRoom
return `${school}, ${room}`;
}
let log = showRoom() + "\n";
log += "Outside, room is: " + (typeof room === "undefined" ? "not defined" : room);
document.getElementById("out").textContent = log;
</script>
Block scope
Variables made with let and const also stay inside the nearest { } block, such as an if or a loop:
if (true) {
const secret = "hidden";
}
console.log(secret); // ReferenceError: secret is not defined
Why scope is a good thing
Scope keeps variables from clashing. Two functions can each have their own variable called total without affecting each other. If everything were global, any function could accidentally overwrite another's data.
Arrow functions
An arrow function is a shorter way to write a function expression, using =>:
// Regular function expression
const square = function (n) {
return n * n;
};
// Arrow function
const square2 = (n) => {
return n * n;
};
// Even shorter: one expression, automatic return
const square3 = (n) => n * n;
Rules for the short form:
- If the body is a single expression, you can drop the
{ }andreturn; the value is returned automatically. - With exactly one parameter, the parentheses are optional:
n => n * n. - With no parameters, use empty parentheses:
() => "Hello!".
<pre id="out"></pre>
<script>
const isPassing = (grade) => grade >= 75;
const peso = (amount) => "₱" + amount.toFixed(2);
const fullName = (first, last) => `${last}, ${first}`;
document.getElementById("out").textContent =
"isPassing(80): " + isPassing(80) + "\n" +
"peso(45): " + peso(45) + "\n" +
"fullName('Jose', 'Rizal'): " + fullName("Jose", "Rizal");
</script>
Callbacks: functions as values
In JavaScript, a function is a value, so you can pass it to another function. A function passed this way is called a callback. You'll use callbacks constantly with events, timers and array methods:
<pre id="out"></pre>
<script>
function applyToAll(grades, action) {
let out = "";
for (const g of grades) {
out += action(g) + "\n";
}
return out;
}
const grades = [72, 88, 95];
document.getElementById("out").textContent =
applyToAll(grades, (g) => `${g} → ${g >= 75 ? "PASSED" : "FAILED"}`);
</script>
applyToAll doesn't know what to do with each grade; the arrow function we pass in decides. Here's a preview of callbacks you'll meet soon:
button.addEventListener("click", () => { ... }); // run when clicked
setTimeout(() => { ... }, 2000); // run after 2 seconds
grades.filter((g) => g >= 75); // keep passing grades
Regular or arrow?
For most small functions and callbacks, arrow functions are shorter and clearer. Regular function declarations are good for main, named functions at the top level of your script. Arrow functions also handle the special word this differently, which matters later when you work with objects and classes.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04