Saturday, November 16, 2024
Google search engine
HomeLanguagesJavascriptJavaScript SyntaxError – Missing = in const declaration

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments