Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Default Parameters

If a function is called with fewer arguments than declared, the missing values are set to undefined.

While this may be acceptable in some cases, it’s often better to assign default values to parameters.

Example

function myFunction(x, y) {
  if (y === undefined) {
    y = 2;
  }
}

Default Parameter Values

ES6 allows function parameters to be assigned default values.

Example

If y is not provided or is undefined, then y will default to 10.

function myFunction(x, y = 10) {
  return x + y;
}
myFunction(5);