Functions: declaration, arrow, return values
A function is a value that runs code when it is called. JavaScript has two ways to write one: a function declaration, which is hoisted to the top of its scope with its body, and an arrow function, which is an expression assigned to a binding and is created where it stands. Both take parameters, both may return a value, and both are values that can be passed to another function.
This is the last lesson of the part. It gathers the forms and the two facts a beginner trips over: a call that returns nothing returns undefined, and a function expression is not hoisted, so reading it before its declaration throws. The next part takes values and scope apart in more detail, starting with references and the call stack.
Declarations and calls
A declaration names the function, lists its parameters and holds its body in braces. The return statement ends the call and hands a value back to the caller; a function without one returns undefined. Because a declaration is hoisted with its body, the call may stand above the declaration in the file, and the program reads top to bottom in the order it runs.
console.log('hoisted:', add(2, 3));
function add(a, b) {
return a + b;
}
const multiply = (a, b) => a * b;
console.log('arrow:', multiply(4, 5));
const greet = (name = 'world', punctuation = '!') => `Hello, ${name}${punctuation}`;
console.log(greet());
console.log(greet('JavaScript', '.'));
function sum(...numbers) {
let total = 0;
for (const number of numbers) {
total += number;
}
return total;
}
console.log('rest:', sum(1, 2, 3, 4));
function noReturn() {
console.log('inside noReturn');
}
console.log('returns:', noReturn());
node functions.js
hoisted: 5
arrow: 20
Hello, world!
Hello, JavaScript.
rest: 10
inside noReturn
returns: undefined
The first line proves the hoisting: add is called before the line that declares it. The line with noReturn() prints undefined for the same reason a declaration at the prompt does: a call with no return produces no value, and the console.log around it prints what it received.
Arrow functions
An arrow function drops the word function and puts an arrow between the parameters and the body: (a, b) => a * b. With one parameter the parentheses may be left out, and with a single expression the braces and the return may be left out too: the expression is the return value. An arrow that needs several statements keeps the braces and writes its own return.
The arrow form is also the one that keeps this from the surrounding code, which matters for methods and callbacks and is the subject of the objects part. For a plain function of two parameters the two forms compute the same answer, as the program shows: 4 times 5 is 20 whichever one is used.
Default and rest parameters
A parameter may carry a default, written with = after its name. The default is used when the argument is undefined or missing, and not when it is null or 0. A rest parameter, written with three dots, collects every remaining argument into an array, which is how a function accepts a number of values it did not know in advance.
In greet() both defaults apply and the result is Hello, world!; in greet("JavaScript", ".") neither does. The sum function takes any number of values and adds them with a loop, and the same job is one call to reduce in the collections part.
Return values and function expressions
A function expression is a function assigned to a binding: const expressed = function () { ... };. The name is a const binding, and the function is created when that line runs. The declaration is therefore not hoisted with its body, and a call above it reaches a binding that is still in its temporal dead zone.
try {
expressed();
} catch (error) {
console.log(error.name + ': ' + error.message);
}
const expressed = function () {
return 'called';
};
console.log('later:', expressed());
node hoisting.js
ReferenceError: Cannot access 'expressed' before initialization
later: called
The error was caught so that the program could continue and show the second line: once the assignment has run, the same name is callable. This is the last program of the part, and it joins the two ideas that part 2 takes further: a binding has a life of its own, and a function is a value like any other.
Exercises
- Rewrite add as an arrow function and move the call to the first line. What changes, and which error does the program throw?
- Give sum a starting value with a default parameter and call it with no arguments. What does it return?
- Change greet("JavaScript") to pass only the name. Which default is used for the punctuation, and what does the line print?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a function return when it has no return statement?
Answer
undefined
A call without a return produces undefined, and console.log around it prints that value.
-
What is hoisted together with its body?
Answer
A function declaration
A declaration is hoisted with its body, while an expression is created where it stands.
-
What does this program print?
console.log(add(2, 3)); function add(a, b) { return a + b; }Answer
5
The declaration is hoisted with its body, so the call above it works.
-
When is a default parameter used?
Answer
When the argument is undefined or missing
Only undefined and a missing argument trigger the default; 0 and null are values and are passed as they are.
-
What does a rest parameter collect?
Answer
Every remaining argument, into an array
The three dots gather the rest of the call into one array, which the body can loop over.
-
What does this program print? ``
const greet = (name = "world") =>Hello, ${name}; console.log(greet());``Answer
Hello, world
The call passes no argument, so the default value fills the parameter.
-
What is printed by a function that ends with
returnand no value?Answer
undefined
A bare return hands back undefined, which is the same value a function without return gives.
-
What happens when a function expression is called before its declaration line?
Answer
It throws a ReferenceError about the temporal dead zone
The const binding is not initialised until its line runs, so the call above it reads the dead zone.
-
How is an arrow function with one expression written?
Answer
(a, b) => a * b
The arrow replaces the word function, and a single expression is returned without braces or return.
-
Which of these is a value that can be passed to another function?
Answer
A function
A function is an object, so it can be stored in a binding, passed as an argument and returned from a call.
The code on this page was run with Node.js 24.22.1 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev