Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

SyntaxError: rest parameter may not have a default

The JavaScript exception "rest parameter may not have a default" occurs when arest parameter has adefault value. Because the rest parameter always creates an array, the default value would never apply.

Message

SyntaxError: Rest parameter may not have a default initializer (V8-based)SyntaxError: rest parameter may not have a default (Firefox)SyntaxError: Unexpected token '='. Expected a ')' or a ',' after a parameter declaration. (Safari)

Error type

What went wrong?

Adefault parameter gives a parameter a default value if the argument is not passed or passed asundefined. Arest parameter collects all the remaining arguments passed to the function and always creates an array. Therefore, it doesn't make sense to have a default value for a rest parameter.

Examples

Invalid cases

js
function doSomething(...args = []) {}

Valid cases

js
function doSomething(...args) {  // args is always an array}

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp