Functions in JavaScript: Hoisting & Parameters
Functions in JavaScript are reusable blocks of code that are also values. A function declaration is hoisted with its body, so it can be called before its line; a function expression or arrow function follows the rules of the variable holding it. Default parameters apply only to undefined, and rest parameters collect extra arguments into an array.
Key Learning Objectives
Write function declarations, function expressions and arrow functions, and know how each is hoisted.
Use default and rest parameters and know when a default applies.
Explain what the arguments object counts and why arrows don't have one.
Return functions from functions and read their length and name.
The Interview Problem
What do the four lines of output say?
1console.log(declared(2));2function declared(n) {3 return n * 2;4}56try {7 expressed(2);8} catch (e) {9 console.log(e.name);10}11const expressed = function (n) {12 return n * 3;13};1415function greet(name = 'guest', ...rest) {16 return `${name}:${rest.length}:${arguments.length}`;17}18console.log(greet(), greet(undefined, 1, 2), greet(null));1920const add = (a) => (b) => a + b;21console.log(add(2)(3), typeof add, add.length);
Predict Console Output
Select the option that matches what this code prints to the console:
4 / ReferenceError / guest:0:0 guest:2:3 null:0:1 / 5 function 1
4 / TypeError / guest:0:0 guest:2:3 null:0:1 / 5 function 1
4 / ReferenceError / guest:0:0 guest:2:3 guest:0:1 / 5 function 1
4 / ReferenceError / guest:0:0 guest:2:2 null:0:1 / 5 function 1
V8 Engine Execution Trace
Step 1 of 4 (Line 1)Before any code runs, the declaration declared is created with its body, so calling it on line 1 works and returns 4.
Functions in JavaScript Explained
How Functions in JavaScript Are Created and Called
Functions in JavaScript are values: you can store them, pass them and return them. There are three common ways to write one: a function declaration (function f() {}), a function expression (const f = function () {}) and an arrow function (const f = () => {}).
- Hoisting: declarations are created before any code runs, with their body, so they can be called earlier in the file. Expressions follow the rules of the variable that holds them: with
constorletthe name is in the temporal dead zone until its line runs (ReferenceError); withvarit isundefined, so calling it is a TypeError. - Parameters: a default (
name = 'guest') applies only when the argument isundefined, missing or passed explicitly. A rest parameter (...rest) collects the remaining arguments into a real array. Regular functions also havearguments, which counts everything passed; arrow functions do not have their own. - Functions returning functions:
add(2)returns a new function that remembersathrough a closure.fn.lengthis the number of parameters before the first default or rest parameter.
Common Traps & Mistakes
Calling a function expression before the line that defines it.
Expecting a default parameter to replace null.
Using arguments inside an arrow function and getting the outer function's arguments (or an error at the top level of a module).
FAANG Follow-Up Probes
Probe #1
What is the difference between a function declaration and a function expression?
Probe #2
Write a function that accepts any number of numbers and returns their sum.
Probe #3
What does fn.length return for function (a, b = 1, ...c) {}?
Functions in JavaScript Interview Questions
Key interview inquiries, edge cases, and architectural follow-ups for Functions.
What is the difference between a function declaration and a function expression?
A declaration (function f() {}) is hoisted with its body, so you can call it before its line. An expression (const f = function () {}) is only available after its line runs, like any other value assigned to a variable.
What are the types of functions in JavaScript?
Function declarations, function expressions (named or anonymous), arrow functions, methods, constructor functions and classes, generator functions (function*) and async functions.
When does a default parameter apply?
Only when the argument is undefined, either missing or passed explicitly. null, 0 and '' are real values and keep their value.
What is the difference between rest parameters and the arguments object?
A rest parameter (...rest) is a real array of the remaining arguments and works in arrow functions. arguments is an array-like object of all arguments, exists only in regular functions and has no array methods.
Can a function return another function?
Yes. add = (a) => (b) => a + b returns a new function that remembers a through a closure, so add(2)(3) is 5. That is the basis of currying and function factories.
What does the length property of a function mean?
The number of declared parameters before the first one with a default value or a rest parameter. (a, b = 1, ...c) => {} has length 1.
More JavaScript interview questions for freshers
