JavaScript SyntaxError – Function statement requires a name

This JavaScript exception function statement requires a name that occurs if there is any function statement in the script which requires a name.

Message:

Syntax Error: Expected identifier (Edge)
SyntaxError: function statement requires a name [Firefox]
SyntaxError: Unexpected token ( [Chrome]

Error Type:

Syntax Error

Cause of Error: Any function statement in the code that needs a name. Just check how functions are defined and if necessary provide a name for it, or if the function in question requires to be a function expression,

Example 1: In this example, the function name is provided, So the error has not occurred.



HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
    <title>Syntax Error</title>
</head>
<body>
    <script>
      var GFG = function () {
        return 'Hello Geek';
      }
      document.write(GFG());
    </script>
</body>
</html>

chevron_right


Output:

Hello Geek

Example 2: In this example, the function name is not provided, So the error occurred.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
   <title>Syntax Error</title>
</head>
<body>
    <script>
      function () {
        return 'Hello Geek';
      }
    </script>
</body>
</html>

chevron_right


Output(in console):

SyntaxError: Function statements require a function name

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.