Avoid default parameters before normal parameters

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

Metadata

ID: javascript-best-practices/default-param-last

Language: JavaScript

Severity: Warning

Category: Best Practices

Description

This rule encourages the practice of defining default parameters after the normal parameters in a function declaration. This is to ensure that the function behaves as expected when it is called with fewer arguments.

Default parameters are used to initialize formal parameters with default values. They are useful when an argument is not provided in the function or if it is undefined. If the function is called with fewer arguments than the declared parameters, the normal parameter receives undefined, while the default parameter is initialized with the provided value.

To avoid this, ensure that normal parameters are always defined before default parameters. This ensures that the function behaves as expected and doesn’t cause any unexpected results.

Non-Compliant Code Examples

function foo(a = false, b) {}
foo(undefined, "b")

Compliant Code Examples

function foo(a, b = false) {}
foo("a")
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis

PREVIEWING: mervebolat/span-id-preprocessing