Syntaxerror missing initializer in const declaration

Today, we are going to deal with syntaxerror missing initializer in const declaration, an error message usually happens in JavaScript and Typescript.

If you’re struggling to fix this error, then keep on reading.

In this article, we’ll walk you through how to fix the uncaught syntaxerror: missing initializer in const declaration.

What is constant?

A constant is a value that cannot be altered by the program during normal execution. It cannot change through re-assignment, and it can’t be redeclared.

In addition to that, constants are declared using the const keyword in JavaScript.

An initializer for a constant is required; that is, you must specify its value in the same statement in which it’s declared (which makes sense, given that it can’t be changed later).

What is “syntaxerror: missing initializer in const declaration”?

The error message syntaxerror missing initializer in const declaration occurs when you declare a variable using const. Unfortunately, you did not initialize its value on the same line.

For example:

const website;

Output:

index.js:1
const website;
      ^^^^^^^

SyntaxError: Missing initializer in const declaration

The error message triggered when a const declaration was not given a value in the same statement. In JavaScript, constants are declared using the const keyword and an initializer for a constant is required.

That is why, you must specify its value in the same statement in which it’s declared.

How to fix “syntaxerror missing initializer in const declaration”?

To fix the syntaxerror: missing initializer in const declaration, you need to provide an initial value for the const declaration in the same statement in which it’s declared.

Solution 1: When using the keyword “const,” ensure to assign the value on the same line

For example:

const website = 'Itsourcecode'; 
console.log(website);

Output:

Itsourcecode

Solution 2: Provide an initial value for the const declaration

The simplest solution is to provide an initial value for the const declaration in the same statement in which it’s declared.

For example:

const COLUMNS;❌

const website = Itsourcecode;✅

Solution 3: Use let statement

Variables declared with the “let” statement can be changed or assigned a new value. However, once a variable is declared using “let,” it cannot be declared again with the same name.

For example:

let website;

website = 'Itsourcecode';
console.log(website)

Output:

Itsourcecode

Conclusion

In conclusion, the error message syntaxerror missing initializer in const declaration occurs when you declare a variable using const. Unfortunately, you did not initialize its value on the same line.

To fix the error you need to provide an initial value for the const declaration in the same statement in which it’s declared.

This article already discussed what this error is all about and multiple ways to resolve this error.

By executing the solutions above, you can master this SyntaxError with the help of this guide.

You could also check out other SyntaxError articles that may help you in the future if you encounter them.

We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊

Leave a Comment