Skip to content

Latest commit

 

History

History
31 lines (23 loc) · 617 Bytes

prefer-default-parameters.md

File metadata and controls

31 lines (23 loc) · 617 Bytes

Prefer default parameters over reassignment

Instead of reassigning a function parameter, default parameters should be used. The foo = foo || 123 statement evaluates to 123 when foo is falsy, possibly leading to confusing behavior, whereas default parameters only apply when passed an undefined value. This rule only reports reassignments to literal values.

This rule is fixable.

Fail

function abc(foo) {
	foo = foo || 'bar';
}
function abc(foo) {
	const bar = foo || 'bar';
}

Pass

function abc(foo = 'bar') {}
function abc(foo) {
	foo = foo || bar();
}