JavaScript Arrow Functions
You already know how to declare a function, pass arguments, and return a value. Many everyday helpers are short enough that the full function keyword feels heavy. JavaScript also lets you write those helpers with an arrow — the => token — in a tighter line.
What an arrow function is
An arrow function is still a function: it takes inputs, runs a body, and can send a result back. The shape changes. You usually assign it to a name with const, list parameters, write =>, then either a short expression or a { } block.
Arrow next to a classic function
📊 Classic function vs arrow
| Idea | Classic style | Arrow style |
|---|---|---|
| named helper | function add(a, b) { return a + b; } |
const add = (a, b) => a + b; |
| return one expression | return a + b; inside { } |
expression after => returns by itself |
| several statements | { } plus return |
{ } plus return — same rule |
| one parameter | (name) or name in older forms |
name => ... (parentheses optional) |
| no parameters | function hi() { ... } |
const hi = () => ... — empty () required |
| where it lives | declaration or expression | almost always stored in a variable |
Both styles can return numbers and strings. Prefer the arrow when the helper is small and you want a clear const name. Prefer a classic function when you want a declaration that stands out as a named block — both are valid JavaScript.
const double = (n) => n * 2;
let answer = double(5);
console.log(answer);
Expression body returns for you
After =>, a single expression is evaluated and returned automatically. No { } and no return keyword. Here double(5) becomes 10. Change the argument; the returned product follows.
const label = (score) => {
if (score < 0) {
return "invalid";
}
return "ok";
};
console.log(label(-1));
console.log(label(8));
Braces mean you control return
When the body uses { }, JavaScript does not auto-return the last line. Write return yourself, just like a classic function. That is how early exits and multi-step logic stay clear.
const greet = name => "Hello, " + name;
let line = greet("Sara");
console.log(line);
One parameter can drop the parentheses
If there is exactly one parameter, you may write name => ... without ( ). The call site is unchanged: greet("Sara"). With two or more parameters, keep the parentheses.
const hello = () => "Hello";
console.log(hello());
Zero parameters still need ()
With no inputs, write empty parentheses before =>. Skipping them is a syntax error. The call still uses hello() — the empty pair marks “no parameters.”
const add = (a, b) => a + b;
let total = add(3, 4);
console.log(total);
console.log(add(10, 2));
Name it, then call it
const add = (a, b) => a + b stores the arrow under add. You call it like any function: add(3, 4) returns 7. Same helper, two calls — reuse without rewriting the body.
Put an arrow on the page
Build a welcome string with an arrow, call it, and write the result into a paragraph — same page pattern as before, shorter function shape.
<!DOCTYPE html>
<html>
<head>
<title>arrow — page</title>
</head>
<body>
<p id="msg">…</p>
<script>
const welcome = guest => "Hello, " + guest;
let box = document.getElementById("msg");
box.textContent = welcome("Omar");
</script>
</body>
</html>
The paragraph shows Hello, Omar. The arrow returned the string; the next line placed it on the page. Change only the argument, refresh, and read the new line. Next lesson covers scope — where those names are visible.
Practice steps
- Rewrite
function triple(n) { return n * 3; }as aconstarrow with an expression body. - Write
const hi = name => "hi " + nameand loghi("Neda"). - Write
const ping = () => "pong"and call it once. - Write a block-body arrow that returns
"zero"whenn === 0, else"nonzero". - Call
add(6, 1)after definingconst add = (a, b) => a + band confirm you get7. - In the HTML demo, change only the argument string, refresh, and read the paragraph.
Frequent slip-ups
- Writing
{ }after=>and forgettingreturn— the call then yieldsundefined - Dropping
()when there are zero parameters - Mixing up
=>with comparison operators — the arrow is function syntax, not “greater than” - Expecting a full lesson on
thishere — binding differences belong in a later page; this page is the compact shape - Thinking arrows replace
returnknowledge — expression bodies still return; block bodies still needreturn
Summary
- Arrow functions use
=>and are usually stored inconst - A bare expression after
=>returns automatically - A
{ }body needs an explicitreturn - One parameter may omit
( ); zero parameters need empty() - Call an arrow by its name like any other function
- Page updates can use a returned string from an arrow
- Next: scope — where names live
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top