Learning objective: Write arrow functions with block and concise bodies, return object literals correctly, pass arrow functions as callbacks, and know when a regular function is better.
An arrow function is a shorter way to write a function expression: const square = (n) => n * n;.
With a concise body (no braces) the expression after => is returned automatically. With a block body (=> { ... }) you must write return yourself.
Brackets around a single parameter are optional (n => n * 2). With zero or several parameters they are required: () => 42, (a, b) => a + b.
Arrow functions shine as short callbacks, functions you pass to other functions: [1, 2, 3].map(n => n * 10).
Arrow functions do not have their own this, arguments, super or new.target; they use the surrounding ones. This makes them ideal inside methods for callbacks, and wrong as object methods that need this.
Arrow functions cannot be used as constructors (new throws) and are not hoisted, because they are expressions.
To return an object literal from a concise body, wrap it in parentheses: id => ({ id }). Without them the braces are read as a block.
Prefer concise bodies for one-expression transformations and named functions (declarations or named consts) for anything that needs several steps or clear stack traces.
05Definitions
Arrow function
A compact function expression written with =>.
Concise body
An arrow function body without braces; its expression is returned.
Block body
An arrow function body in braces; it needs an explicit return.
Callback
A function passed to another function to be called later.
Lexical this
Arrow functions use the this of the code around them.
3. When are brackets around arrow parameters required?
Always
With zero or two or more parameters
Never
Only with strings
4. Why does make() return undefined?
const make = (id) => { id };
console.log(make(1));
Arrows cannot return objects
The braces are read as a block
id is not defined
It should use function
5. What is a callback?
A function that calls itself
A function passed to another function
A loop
A return value
6. An object method uses this.name. Which form fits?
An arrow function
A regular method
Either
Neither
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Find the missing return: Prints 40; Returns a value
Convert to arrow functions: Prints $12.50; No function keyword
Product label maker: Prints the labels; toLabel works alone; Uses map
ex-024-01: output matches the documented expected output
ex-024-02: output matches the documented expected output
ex-024-03: output matches the documented expected output
ex-024-04: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-01-foundations/module-05-functions-and-reusable-logic/lesson-024-arrow-functions/. Every example on this page is generated from those files.
The GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Academy) has not been published yet, so these links will work once it is. Until then, use the ZIP download above.
15Summary and next steps
Arrows are compact function expressions.
Concise bodies return automatically; block bodies need return.
Wrap returned object literals in parentheses.
Use arrows for short callbacks, regular functions for methods that need this.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationArrow functions
Context: I know function expressions.
Task: Explain arrow syntax, concise and block bodies with five examples.
Constraints: Beginner level.
Expected behaviour: I can convert a function to an arrow.
Testing: Give me four to convert.
Code predictionArrow drill
Context: I know arrow functions.
Task: Give me six arrows, some returning undefined by mistake, and ask what each returns.
Constraints: Wait for answers.
Expected behaviour: Include an object return.
Testing: Explain any I miss.
DebuggingUndefined from an arrow
Context: My arrow function returns undefined.
Task: Help me find why with questions.
Constraints: No direct answer.
Expected behaviour: I fix it myself.
Testing: List the two common causes.
Exercise generationCallbacks
Context: I finished the label maker.
Task: Create three exercises using arrow callbacks with map and filter.
Constraints: Fictional data, exact output.
Expected behaviour: Each needs a named arrow function.
Testing: Include expected outputs.
Code reviewArrow style
Context: My code mixes arrows and declarations randomly.
Task: Suggest a consistent style and explain it.
Constraints: Keep behaviour identical.
Expected behaviour: Each choice has a reason.
Testing: Give me a short style guide.