Skip to content

noShadow

Disallow variable declarations from shadowing variables declared in the outer scope.

Shadowing is the process by which a local variable shares the same name as a variable in its containing scope. This can cause confusion while reading the code and make it impossible to access the global variable.

See also: noShadowRestrictedNames

const foo = "bar";
if (true) {
const foo = "baz";
}
code-block.js:3:10 lint/nursery/noShadow ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This variable shadows another variable with the same name in the outer scope.

1 │ const foo = “bar”;
2 │ if (true) {
> 3 │ const foo = “baz”;
^^^
4 │ }
5 │

This is the shadowed variable, which is now inaccessible in the inner scope.

> 1 │ const foo = “bar”;
^^^
2 │ if (true) {
3 │ const foo = “baz”;

Consider renaming this variable. It’s easy to confuse the origin of variables if they share the same name.

Variable declarations in functions can shadow variables in the outer scope:

const foo = "bar";
const bar = function () {
const foo = 10;
}
code-block.js:3:11 lint/nursery/noShadow ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This variable shadows another variable with the same name in the outer scope.

1 │ const foo = “bar”;
2 │ const bar = function () {
> 3 │ const foo = 10;
^^^
4 │ }
5 │

This is the shadowed variable, which is now inaccessible in the inner scope.

> 1 │ const foo = “bar”;
^^^
2 │ const bar = function () {
3 │ const foo = 10;

Consider renaming this variable. It’s easy to confuse the origin of variables if they share the same name.

Function argument names can shadow variables in the outer scope:

const foo = "bar";
function bar(foo) {
foo = 10;
}
code-block.js:2:14 lint/nursery/noShadow ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This variable shadows another variable with the same name in the outer scope.

1 │ const foo = “bar”;
> 2 │ function bar(foo) {
^^^
3 │ foo = 10;
4 │ }

This is the shadowed variable, which is now inaccessible in the inner scope.

> 1 │ const foo = “bar”;
^^^
2 │ function bar(foo) {
3 │ foo = 10;

Consider renaming this variable. It’s easy to confuse the origin of variables if they share the same name.

const foo = "bar";
if (true) {
const qux = "baz";
}
biome.json
{
"linter": {
"rules": {
"nursery": {
"noShadow": "error"
}
}
}
}