Skip to content

Latest commit

 

History

History
99 lines (65 loc) · 1.45 KB

no-const-assign.md

File metadata and controls

99 lines (65 loc) · 1.45 KB
title layout edit_link rule_type
no-const-assign
doc
problem

We cannot modify variables that are declared using const keyword. It will raise a runtime error.

Under non ES2015 environment, it might be ignored merely.

Rule Details

This rule is aimed to flag modifying variables that are declared using const keyword.

Examples of incorrect code for this rule:

::: incorrect

/*eslint no-const-assign: "error"*/
/*eslint-env es6*/

const a = 0;
a = 1;

:::

::: incorrect

/*eslint no-const-assign: "error"*/
/*eslint-env es6*/

const a = 0;
a += 1;

:::

::: incorrect

/*eslint no-const-assign: "error"*/
/*eslint-env es6*/

const a = 0;
++a;

:::

Examples of correct code for this rule:

::: correct

/*eslint no-const-assign: "error"*/
/*eslint-env es6*/

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

:::

::: correct

/*eslint no-const-assign: "error"*/
/*eslint-env es6*/

for (const a in [1, 2, 3]) { // `a` is re-defined (not modified) on each loop step.
    console.log(a);
}

:::

::: correct

/*eslint no-const-assign: "error"*/
/*eslint-env es6*/

for (const a of [1, 2, 3]) { // `a` is re-defined (not modified) on each loop step.
    console.log(a);
}

:::

When Not To Use It

If you don't want to be notified about modifying variables that are declared using const keyword, you can safely disable this rule.