JavaScript SyntaxError – Missing = in const declaration

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 initalized (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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

SyntaxError: Const must be initalized

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

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

SyntaxError: Const must be initalized

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.