Step: Basics | Player: 04_Control statement
Learning Objectives
- You can write function declarations, function expressions, and arrow functions.
- The default value of the parameter and the remaining parameters (rest) can be used.
- Can explain the difference between argument and parameter.
- Distinguish between function scope and block scope.
- Understand the concept of pure function.
1. Function declaration vs expression vs arrow
Function declarations can be hoisted and called from above. Expressions and arrow functions can be used after they are assigned to variables. Arrow functions are short and do not have their own this , making them suitable for callbacks.
function add(a, b) { return a + b; } //declaration
const sub = function (a, b) { return a - b; }; //expression
const mul = (a, b) => a * b; //arrow
2. Parameters and arguments
You can specify default values for parameters and receive variable arguments with ...rest.
function greet(name = "friend") {
return `Hello, ${name}!`;
}
function sum(...nums) {
return nums.reduce((a, b) => a + b, 0);
}
3. Return and side effects
The function explicitly returns a value of return; if not, it is undefined. A function that always returns the same result given the same input and does not change the external state is called a "pure function."
//pure
const square = (x) => x * x;
//Impure: change of external state
let counter = 0;
const tick = () => { counter += 1; };
4. Scope
Variables are visible only within the block in which they are declared. let/const is a block scope and var is a function scope. Referencing an outer variable inside a function is called a closure (more on this in the next step).
function outer() {
const a = 1;
if (true) {
const b = 2;
console.log(a, b); // 1 2
}
// console.log(b); // ReferenceError
}
Core example
| file | Contents covered |
|---|---|
01_declaration.js |
Function declarations and hoisting |
02_expression.js |
Function expressions and arrow functions |
03_params.js |
Defaults, remaining parameters, destructuring |
04_scope.js |
Block/Function Scope and variable access range |
⚠️ Common Mistakes
- In an arrow function, a bug is created by forgetting that
thisis an externalthisand defining it as a method. - When passing
null, forgetting that the default parameter value is applied only toundefined. - If you omit
returnand do not know thatundefinedis returned. - Missing parentheses when returning an object literal directly from an arrow function (
() => ({ a: 1 })). - When a function secretly changes external variables, creating a bug that is difficult to trace.
❓ Frequently Asked Questions (FAQ)
Q1. Which should I use: a declaration or an expression? A. The declaration for the utility defined at the top of the module is easy to read, and the callback/local function has concise arrows.
Q2. What if there are too many arguments? A. Bundle them into an object. Like function create({ name, age, role }) { ... }, it utilizes structural decomposition.
Q3. Are functions also values? A. Yes. You can assign it to a variable, pass it as an argument to another function, and return it as a result. These are called "first-class functions".