JavaScript: Function Parameters

By Xah Lee. Date: . Last updated: .

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

If there are more arguments passed in a function call than declared parameters, they are ignored.

If less, then extra parameters have values of undefined.

function f (x) { return x;}

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

Rest Parameters

ES2015 added Rest Parameters feature. It allows you to explicitly declare optional parameters, and they are passed in as a array.

〔►see JavaScript: Function Rest Parameters

Declare Parameter Default Value

To declare default value for parameter, use ES2015 feature for default arguments.

〔►see JavaScript: Function Argument Default Value

For pre-ES2015, to define default values for optional parameter, you can just check if a argument is undefined, then set value.

// define function with optional parameter and default value

function f (x, y) {
    if ( y === undefined ) {
        y = 3; // default value
    return x + y;

console.log(f(2)); // 5

the 「arguments」 Object

〔►see JavaScript: Function's “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 JavaScript: Function's “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.

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

With ES2015 onward, you should avoid using this feature.

ES2015 Features

ES2015 has many features that obsolete the need for the argument object. See:

Function Topic

  1. JavaScript: Define Function
  2. JavaScript: Function Parameters
  3. JavaScript: Variable/Function Declaration Order, Name Hoisting
  4. JavaScript Variable Scope: Function Level vs Block Level
  5. JavaScript: Function Declaration vs Function Expression
  6. JavaScript: Closure
  7. JavaScript: Functional Programing
  8. JavaScript: Function Call, Apply, Bind
  9. JavaScript: Function Argument Default Value
  10. JavaScript: Function Rest Parameters
  11. JavaScript: Arrow Function

  1. JavaScript: Function Object
  2. JavaScript: Function.prototype
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.