Sub topic 2
Function Declarations
This chapter assumes you know basic JavaScript syntax, how var, let, and const declare variables, and how to call a function with arguments. It covers the oldest and most common way to define a function: the function declaration.
The Shape of a Declaration
A function declaration is a statement. It begins with the function keyword, followed by a required name, a parameter list in parentheses, and a body in braces.
function add(a, b) {
return a + b;
}
console.log(add(2, 3)); // 5When JavaScript reads this statement, it creates a function object and binds it to the name add in the enclosing scope. Functions are values, so add is now a variable that holds a function, and you can pass it around like any other value.
Statement Position Matters
A declaration has to appear where a statement is allowed: at the top level of a script or module, or directly inside a function body. If function appears where JavaScript expects an expression, such as after = or inside parentheses, it is parsed as a function expression instead. The next chapter covers expressions.
The name is mandatory. This is a syntax error:
function (a, b) {
return a + b;
}The only exception is export default function () {} in a module, where the name may be left out.
Return Values
A function returns the value of the first return statement it reaches. If execution reaches the end of the body without a return, or hits a bare return;, the call evaluates to undefined.
function logOnly(message) {
console.log(message);
}
function early(n) {
if (n < 0) return;
return n * 2;
}
console.log(logOnly('hi')); // logs "hi", then undefined
console.log(early(-1)); // undefined
console.log(early(4)); // 8Useful Properties
Every function declaration produces an object with a few properties you can inspect:
| Property | Meaning | `add` above |
|---|---|---|
| `name` | The declared name, as a string | `"add"` |
| `length` | Number of parameters before the first default or rest parameter | `2` |
| `prototype` | Object used when the function is called with `new` | an object |
function add(a, b) {
return a + b;
}
console.log(add.name); // "add"
console.log(add.length); // 2
console.log(typeof add.prototype); // "object"The name property is what shows up in stack traces, which makes declarations easy to find when debugging. The prototype property exists because declarations can act as constructors. A later chapter contrasts this with arrow functions, which have no prototype.
Scope of the Name
The name is scoped like a variable declared in the surrounding function or module. Inside a function, a declaration is local to that function:
function outer() {
function helper(x) {
return x * 10;
}
return helper(3);
}
console.log(outer()); // 30
console.log(typeof helper); // "undefined"Because the name is an ordinary binding, it can be reassigned later in non-strict code (add = 5), which silently replaces the function. Linters usually flag this as no-func-assign.
Declarations also have one behavior that expressions lack: you can call them before the line where they appear. The "Hoisting" chapter explains why.
Exercise
What does this program log, and why? function check(n) { if (n > 10) { return 'big'; } } console.log(check(5));
Show answer
It logs undefined. Since 5 is not greater than 10, the function never reaches a return statement, and a function that ends without returning gives back undefined.
Exercise
Given function greet(name, greeting = 'Hi') { return greeting + ' ' + name; }, what are greet.name and greet.length?
Show answer
greet.name is "greet" and greet.length is 1, because length counts only the parameters before the first one that has a default value.