# block-scoped-var

Rule `eslint/block-scoped-var`. Enforce the use of variables within the scope they are defined

- Source: eslint
- Technologies: JavaScript
- Kind: suspicious
- Needs type information: no
- Autofix: no
- Reach: 13.7M weekly downloads of presets that enable it
- Trend: +14.5% last quarter
- Rank: #686 by reach, #1410 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `block-scoped-var` | no | no | package `eslint` | [docs](https://eslint.org/docs/latest/rules/block-scoped-var) |
| Oxlint | `block-scoped-var` | no | no | plugin `eslint`, category suspicious | [docs](https://oxc.rs/docs/guide/usage/linter/rules/eslint/block-scoped-var.html) |

## Code examples

### Incorrect: the rule reports this

```js
function doIf() {
    if (true) {
        var build = true;
    }

    console.log(build);
}

function doIfElse() {
    if (true) {
        var build = true;
    } else {
        var build = false;
    }
}

function doTryCatch() {
    try {
        var build = 1;
    } catch (e) {
        var f = build;
    }
}

function doFor() {
    for (var x = 1; x < 10; x++) {
        var y = f(x);
    }
    console.log(y);
}

class C {
// …
```

### Correct: the rule accepts this

```js
function doIf() {
    var build;

    if (true) {
        build = true;
    }

    console.log(build);
}

function doIfElse() {
    var build;

    if (true) {
        build = true;
    } else {
        build = false;
    }
}

function doTryCatch() {
    var build;
    var f;

    try {
        build = 1;
    } catch (e) {
        f = build;
    }
}

function doFor() {
// …
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
export default [
  {
    rules: {
      "block-scoped-var": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

```json
{
  "rules": {
    "block-scoped-var": "error"
  }
}
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Airbnb base](https://lintradar.dev/presets/airbnb-base.md) | ESLint | `eslint-config-airbnb-base` | 241 | 6.5M | +2.1% |
| [Airbnb](https://lintradar.dev/presets/airbnb.md) | ESLint | `eslint-config-airbnb` | 346 | 3.4M | +2.3% |
| [Ultracite for Oxlint](https://lintradar.dev/presets/ultracite-oxlint.md) | Oxlint | `ultracite` | 475 | 2.1M | +151.7% |
| [Anthony Fu's config](https://lintradar.dev/presets/antfu.md) | ESLint | `@antfu/eslint-config` | 294 | 758.7K | +49.9% |
| [Google TypeScript Style](https://lintradar.dev/presets/gts.md) | ESLint | `gts` | 71 | 553.3K | +10.3% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [sonarjs/block-scoped-var](https://lintradar.dev/rules/sonarjs/block-scoped-var.md): Variables should be used in the blocks where they are declared
- [eslint/no-alert](https://lintradar.dev/rules/eslint/no-alert.md): Disallow the use of `alert`, `confirm`, and `prompt`
- [eslint/no-restricted-syntax](https://lintradar.dev/rules/eslint/no-restricted-syntax.md): Disallow specified syntax
- [eslint/camelcase](https://lintradar.dev/rules/eslint/camelcase.md): Enforce camelcase naming convention
- [eslint/no-restricted-properties](https://lintradar.dev/rules/eslint/no-restricted-properties.md): Disallow certain properties on certain objects
- [eslint/strict](https://lintradar.dev/rules/eslint/strict.md): Require or disallow strict mode directives

---

Web page: https://lintradar.dev/rules/eslint/block-scoped-var. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
