Open In App

JavaScript SyntaxError – Missing = in const declaration

Improve
Improve
Like Article
Like
Save
Share
Report

This JavaScript exception missing = in const declaration occurs if a const is declared and value is not provided(like const ABC_DEF;). Need to provide the value in same statement (const ABC_DEF = ‘#ee0’).

Message:

SyntaxError: Const must be initialized (Edge)
SyntaxError: missing = in const declaration (Firefox)
SyntaxError: Missing initializer in const declaration (Chrome)

Error Type:

SyntaxError

Cause of Error: A constant value cannot be changed by the program while execution. It cannot be altered through re-assignment also.

Example 1: In this example, a const is declared but not initialized so the error has occurred.

HTML




<!DOCTYPE html>
<html>
<head>
    <title>Syntax Error</title>
</head>
<body>
    <script>
      const GFG;                 
      document.write(GFG);
    </script>
</body>
</html>


Output:

SyntaxError: Const must be initialized

Example 2:  In this example, a const is declared and initialized later, so the error has occurred.

HTML




<!DOCTYPE html>
<html>
<head>
    <title>Syntax Error</title>
</head>
<body>
    <script>
      const INIT_VAL;
      // invalid statement
      INIT_VAL = 5;                 
      document.write(INIT_VAL);
    </script>
</body>
</html>


Output:

SyntaxError: Const must be initialized


Last Updated : 27 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads