Skip to content

eslint/block-scoped-var Suspicious ​

What it does ​

Generates warnings when variables are used outside of the block in which they were defined. This emulates C-style block scope.

Why is this bad? ​

This rule aims to reduce the usage of variables outside of their binding context and emulate traditional block scope from other languages. This is to help newcomers to the language avoid difficult bugs with variable hoisting.

Examples ​

Examples of incorrect code for this rule:

js
function doIf() {
  if (true) {
    var build = true;
  }
  console.log(build);
}

Examples of correct code for this rule:

js
function doIf() {
  var build;
  if (true) {
    build = true;
  }
  console.log(build);
}

How to use ​

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny block-scoped-var
json
{
  "rules": {
    "block-scoped-var": "error"
  }
}

References ​

Released under the MIT License.