JavaScript: Function Parameters

By Xah Lee. Date: . Last updated: .

Number of Arguments Not Checked

JavaScript doesn't check the number of arguments passed in a function call.

function ff(x) {
  return x;
}

// extra arguments are ignored
console.log(ff(2, 3, 4) === 2);
const gg = ((x) => x);

// extra arguments are ignored
console.log(gg(2, 3, 4) === 2);
// unfilled parameters have value of undefined
function gg(x, y) {
  return y;
}
console.log(gg(3) === undefined);
// unfilled parameters have value of undefined
const gg = ((x, y) => y);
console.log(gg(3) === undefined);

Parameter Default Value

Parameter can have default value, like this:

function f(param=value) {…}

Function Argument Default Value

Rest Parameters

A function can have unspecified number of parameters, like this:

function f (...name) { return name; }

Function Rest Parameters

Function Argument Destructure

Function Argument Destructure

the γ€Œarguments」 Object

arguments Object

How to find the number of arguments passed?

The number of arguments passed is arguments.length.

With ES2015 onward, you should avoid using this feature.

[see arguments Object]

How to find out how many parameters are declared?

Function has a .length property. Its value is the number of parameters declared in the function.

console.log(
  (function (x, y) {
    return x + y;
  }).length,
);
// 2

With ES2015 onward, you should avoid using this feature.

JavaScript Function

BUY
Ξ£JS
JavaScript in Depth

JavaScript in Depth

Basic Syntax

Value Types

Variable

String

Property

Object and Inheritance

Array

Function

Constructor/Class

Iterable 🌟

Misc