Disallow reassigning const variables

This page is not yet available in Spanish. We are working on its translation.
If you have any questions or feedback about our current translation project, feel free to reach out to us!

Metadata

ID: javascript-best-practices/no-const-assign

Language: JavaScript

Severity: Error

Category: Best Practices

Description

In JavaScript, assigning to a const variable is an error and causes an exception to be thrown at runtime. This rule disallows assigning to constant variable declarations.

Non-Compliant Code Examples

const a = 0;
a = 1;

const b = 0;
b += 1;

const c = 0;
++c;

Compliant Code Examples

const a = 0;
console.log(a);

for (const a in [1, 2, 3]) { // `a` is re-defined (not modified) on each loop step.
    console.log(a);
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis