Home/Concepts/Functions
⚙️
Intermediate⏱ 12 min#06 / 08

Functions

Reusable Code Blocks

Functions are reusable, named blocks of code that perform a task. They are the fundamental building blocks of JavaScript programs - enabling code reuse, abstraction, and clean organization.

Your Progress0/8 complete
0 visited0%
💡 Real-World Analogy

A function is like a vending machine. You put in inputs (parameters), it runs its logic inside, and returns output. You can use the same machine countless times with different inputs.

🔑 Key Concepts

Function Declaration

Classic syntax - hoisted, so can be called before defined. function name(params) { return value; }

Arrow Functions

Concise ES6 syntax: const fn = (params) => expression. Great for callbacks.

Parameters & Return

Parameters are inputs, return sends output. Default params, rest params (...args), destructuring all work.

💻 Code Examples

1Three Ways to Write Functions
functions.js
1// 1. Function Declaration (hoisted)
2function greet(name) {
3 return "Hello, " + name + "!";
4}
5greet("Thunder"); // "Hello, Thunder!"
6 
7// 2. Function Expression (NOT hoisted)
8const add = function(a, b) {
9 return a + b;
10};
11add(5, 3); // 8
12 
13// 3. Arrow Function (ES6 - concise)
14const multiply = (a, b) => a * b;
15multiply(4, 5); // 20
16 
17// Single param - no parentheses needed
18const double = n => n * 2;
19double(7); // 14
20 
21// Multi-line arrow function
22const getGrade = score => {
23 if (score >= 90) return "A";
24 if (score >= 80) return "B";
25 return "C";
26};
2Parameters, Defaults & Rest
functions.js
1// Default parameters
2function introduce(name, role = "Student") {
3 return name + " is a " + role;
4}
5introduce("Alex"); // "Alex is a Student"
6introduce("Sam", "Teacher"); // "Sam is a Teacher"
7 
8// Rest parameters - collect extras into array
9function sum(...numbers) {
10 return numbers.reduce((total, n) => total + n, 0);
11}
12sum(1, 2, 3); // 6
13sum(1, 2, 3, 4, 5); // 15
14 
15// Return multiple values via object
16function minMax(arr) {
17 return { min: Math.min(...arr), max: Math.max(...arr) };
18}
19const { min, max } = minMax([3, 1, 7, 2, 9]);
20// min = 1, max = 9
3Higher-Order Functions & Closures
functions.js
1// Higher-order: function takes another function
2function applyTwice(fn, value) {
3 return fn(fn(value));
4}
5const double = x => x * 2;
6applyTwice(double, 5); // 20 (5 -> 10 -> 20)
7 
8// Closures - function remembers its outer scope
9function makeCounter() {
10 let count = 0; // private variable
11 return {
12 increment: () => ++count,
13 decrement: () => --count,
14 value: () => count,
15 };
16}
17const counter = makeCounter();
18counter.increment(); // 1
19counter.increment(); // 2
20counter.decrement(); // 1
21counter.value(); // 1

⚠️ Common Mistakes

❌ Mistake

Forgetting return - function silently gives undefined

✅ Fix

If you need output, always use the return keyword. A function without return gives undefined.

❌ Mistake

Writing greet instead of greet() to call it

✅ Fix

greet is just a reference to the function. greet() with parentheses actually CALLS it.

❌ Mistake

Arrow functions and "this" in object methods

✅ Fix

Arrow functions inherit "this" from outer scope. Use regular functions for object methods needing "this".

📋 Quick Reference

function name(params) { return val; }const name = params => expressionDefault: param = defaultValueRest: ...args -> arrayClosures: inner fn sees outer varsHoisting: declarations only

Live Code Playground

Edit the code and press ▶ Run to see output instantly.

functions_playground.js
⟳ Loading editor...
💾 Auto-saved
// Press ▶ Run Code to execute and view output console logs...
📋
← Previous
Arrays
Challenge Mode
Next →
Conditionals
🔀