Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

SyntaxError: function statement requires a name

The JavaScript exception "function statement requires a name" occurswhen there is afunction statementin the code that requires a name.

Message

SyntaxError: Function statements require a function name (V8-based)SyntaxError: function statement requires a name (Firefox)SyntaxError: Function statements must have a name. (Safari)

Error type

What went wrong?

There is afunction statement in the code that requires a name.You'll need to check how functions are defined and if you need to provide a name for it, or if the function in question needs to be a function expression, anIIFE, or if the function code is placed correctly in this context at all.

Examples

Statements vs. expressions

Afunction statement (orfunction declaration) requires a name.This won't work:

js
function () {  return "Hello world";}// SyntaxError: function statement requires a name

You can use afunction expression (assignment) instead:

js
const greet = function () {  return "Hello world";};

If your function is intended to be anIIFE (Immediately Invoked Function Expression, which is a function that runs as soon as it is defined) you will need to add a few more braces:

js
(function () {  // …})();

Labeled functions

Labels are an entirely different feature from function names. You can't use a label as a function name.

js
function Greeter() {  german: function () {    return "Moin";  }}// SyntaxError: function statement requires a name

In addition, labeled function declarations themselves are a deprecated feature. Use regular function declarations instead.

js
function Greeter() {  function german() {    return "Moin";  }}

Object methods

If you intended to create a method of an object, you will need to create an object.The following syntax without a name after thefunction keyword is valid then.

js
const greeter = {  german: function () {    return "Moin";  },};

You can also use themethod syntax.

js
const greeter = {  german() {    return "Moin";  },};

Callback syntax

Also, check your syntax when using callbacks.Braces and commas can quickly get confusing.

js
promise.then(  function () {    console.log("success");  });  function () {    console.log("error");}// SyntaxError: function statement requires a name

Correct would be:

js
promise.then(  function () {    console.log("success");  },  function () {    console.log("error");  },);

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp