Stop 06 · JavaScript BasicsTopics · 7/10

Functions

Part of the Web Development Roadmap

In short

Write the logic once, use it a hundred times. Functions let you name a task, give it inputs and get a result back, and you'll write them every single day.

Functions are reusable blocks of code that take inputs called parameters and can return a result. Arrow functions are a shorter way to write them.

The jaadu mascot pours chai from a kettle into two small glasses at a roadside stall.

Think of it like this

Ordering at the chai stall

You tell bhaiya "two cutting, less sugar". Those are the inputs. He follows the same recipe every time and hands you chai, the return value. You don't care how he makes it, only what goes in and what comes out.

Watch it happen

Calling a function

Your code
chai()
samosa()

chai(2)

1/5Your code calls chai with one input, cups = 2.

The big ideas

What you need to know

Three ways to write one

Function declarations, function expressions and arrow functions. Arrows are short and common for small callbacks.

Parameters and defaults

Parameters are the inputs. Give them default values like sugar = "normal" so callers can skip them.

return sends the result

Without a return, a function gives back undefined. return also stops the function right there.

Functions are values

You can store a function in a variable, pass it to another function, or return it. This is used everywhere in JavaScript.

See it in code

Walk through the code

js
1function chai(cups, sugar = "normal") {2  return `${cups} cups, ${sugar} sugar`;3}4 5const samosa = (count) => count * 15;   // arrow function, returns automatically6 7console.log(chai(2));          // "2 cups, normal sugar"8console.log(chai(1, "less"));  // "1 cups, less sugar"9console.log(samosa(4));        // 6010 11function noReturn() { const total = 5; }12console.log(noReturn());       // undefined

1/4A function declaration with a default for sugar. It builds and returns a string.

An arrow function without curly braces returns its expression automatically. Add braces and you must write return yourself.

Save yourself the pain

Common mistakes

Oops

Writing (n) => { n * 2 } and wondering why you get undefined.

Do this instead

With braces you need return: (n) => { return n * 2 }. Or drop the braces: (n) => n * 2.

Oops

One giant function that fetches, calculates and updates the page.

Do this instead

Split it into small functions that each do one job. They're easier to test and reuse.

Your mission

Build a mini bill calculator

~20 min

Quick check

What does this log? const double = (n) => { n * 2 }; console.log(double(5));

Go deeper

  1. 1Write a function that calculates GST on a price and returns the total.
  2. 2Rewrite the same function as an arrow function.
  3. 3Keep each function doing one job and give it a verb name like formatPrice.

Also part of these roadmaps

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge