jskatas.org Continuously Learn JavaScript. Your Way.

Default parameters: basics

Default parameters make function parameters more flexible.

Default parameters make function parameters more flexible

define it using an assignment to the parameter function(param=1){}
let aNumber = (int) => int; assert.equal(aNumber(), 0);
it is used when undefined is passed
let aNumber = (int = 23) => int; const param = 42; assert.equal(aNumber(param), 23);
it is not used when a value is given
function xhr() { return method; } assert.equal(xhr('POST'), 'POST');
it is evaluated at run time
let defaultValue; function xhr(method = `value: ${defaultValue}`) { return method; } assert.equal(xhr(), 'value: 42');
it can also be a function
const defaultValue = 0; function fn(value = defaultValue()) { return value; } assert.equal(fn(), 'defaultValue');

Required Knowledge

Related Katas

Default parameters

  • basics

Difficulty Level

BEGINNER

First Published

30 June 2015

Stats

5 tests to solve