Functions in JavaScript: Hoisting & Parameters

Easy100% Free~12 mins#functions#function-declaration#default-parameters#rest-parameters#hoisting
Interview Answer (30-Second Summary)

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}
5
6try {
7 expressed(2);
8} catch (e) {
9 console.log(e.name);
10}
11const expressed = function (n) {
12 return n * 3;
13};
14
15function greet(name = 'guest', ...rest) {
16 return `${name}:${rest.length}:${arguments.length}`;
17}
18console.log(greet(), greet(undefined, 1, 2), greet(null));
19
20const add = (a) => (b) => a + b;
21console.log(add(2)(3), typeof add, add.length);
Predict Console Output
Interactive Challenge

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.

Call Stack (Top = Active)
Global
declared(2)
Lexical Scope / Bindings
declared:function (hoisted)
Console Stream
> 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 = () => {}).

  1. 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 const or let the name is in the temporal dead zone until its line runs (ReferenceError); with var it is undefined, so calling it is a TypeError.
  2. Parameters: a default (name = 'guest') applies only when the argument is undefined, missing or passed explicitly. A rest parameter (...rest) collects the remaining arguments into a real array. Regular functions also have arguments, which counts everything passed; arrow functions do not have their own.
  3. Functions returning functions: add(2) returns a new function that remembers a through a closure. fn.length is 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.

Q1.
What are functions in JavaScript?

Reusable blocks of code you can call with arguments and that can return a value. Functions are first-class values: they can be stored in variables, passed to other functions and returned from them.

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.

Function declarations, function expressions (named or anonymous), arrow functions, methods, constructor functions and classes, generator functions (function*) and async functions.

Only when the argument is undefined, either missing or passed explicitly. null, 0 and '' are real values and keep their value.

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.

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.

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