Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Functions and parameters: declarations and hoisting

A function groups code for repeated calls. Learn the difference between parameters and arguments, how return values work, and how hoisting lets you call a declared function before its position in the source.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

function name(parameter) { return parameter; }
name(argument);

A function is an object that can be called. The declaration function name(parameter) { return parameter; } creates a named function; name(argument) calls it. A parameter is a name in the definition, whereas an argument is a value supplied by a call. return ends function execution and passes a result to the caller; an ordinary function that reaches its end without return returns undefined.

Function declarations and hoisting: a declaration at the top level of a script or module, or directly inside a function body, is initialized before that scope's statements execute. It can therefore be called before its declaration appears. The code is not physically moved. Inside blocks, strict-mode declarations are block-scoped; outside strict mode, historical compatibility rules complicate their behavior.

In the declaration const add = function(a, b) { return a + b; };, only the initializer function(a, b) { return a + b; } is a function expression. Here the function is assigned only when the initializer executes, and reading add before initialization causes a ReferenceError. Replacing const with var does not allow an earlier call: its value before assignment is undefined.

In the example, a and b are parameters. The default b = 1 applies when the argument is omitted or explicitly undefined, but not for null. The first call demonstrates hoisting; the other two demonstrate the default. Default parameters belong to ECMAScript 2015; the example uses no Web APIs.

Examples

"use strict";

console.log(add(2, 3));
console.log(add(4));
console.log(add(4, undefined));

function add(a, b = 1) {
  return a + b;
}

Result

5
5
5

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources