Skip to content
Related Articles

Related Articles

JavaScript SyntaxError – Function statement requires a name
  • Last Updated : 23 Jul, 2020

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




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

Output:

Hello Geek

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

HTML




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

Output(in console):

SyntaxError: Function statements require a function name
My Personal Notes arrow_drop_up
Recommended Articles
Page :