DEV Community

Cover image for Const
Rakshambika
Rakshambika

Posted on

Const

const:

  • const is a keyword used to declare variables in JavaScript.
  • Variables declared with const have Block Scope.

In const, we must initialize the variable at the time of the declaration itself

<script>

const animal="Lion";

</script>
Enter fullscreen mode Exit fullscreen mode

1.A const variable cannot be declared without a value. Otherwise it will throw SyntaxError.

The error is shown directly in the code editor as a red underline, indicating that the const variable has been declared without an initial value.

Error Message:

2.A value assigned to a const variable cannot be reassigned.

<script>
        const animal="Lion";
        animal="Tiger";
        console.log(animal);
</script>
Enter fullscreen mode Exit fullscreen mode

Error Message:

What is a TypeError in JavaScript?

A TypeError happens when you try to use a value in a way that its data type does not support.

3. Redeclaration is not allowed in const.

The error is shown directly in the code editor as a red underline, indicating that the const variable cannot be redeclared.

Excited to continue learning and building with JavaScript!

Top comments (0)