# vars-on-top

Rule `eslint/vars-on-top`. Require `var` declarations be placed at the top of their containing scope

- Source: eslint
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: no
- Reach: 12.7M weekly downloads of presets that enable it
- Trend: +15.4% last quarter
- Rank: #736 by reach, #1402 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `vars-on-top` | no | no | package `eslint` | [docs](https://eslint.org/docs/latest/rules/vars-on-top) |
| Oxlint | `vars-on-top` | no | no | plugin `eslint`, category style | [docs](https://oxc.rs/docs/guide/usage/linter/rules/eslint/vars-on-top.html) |
| Biome | `style/useVarsOnTop` | no | no | group style, since 2.4.12 | [docs](https://biomejs.dev/linter/rules/use-vars-on-top/) |

## Code examples

### Incorrect: the rule reports this

```js
// Variable declaration in a nested block, and a variable declaration after other statements:
function doSomething() {
    if (true) {
        var first = true;
    }
    var second;
}

// Variable declaration in for initializer:
function doSomethingElse() {
    for (var i=0; i<10; i++) {}
}
```

```js
// Variable declaration after other statements:
f();
var a;
```

```js
// Variables in class static blocks should be at the top of the static blocks.

class C {

    // Variable declaration in a nested block:
    static {
        if (something) {
            var a = true;
        }
    }

    // Variable declaration after other statements:
    static {
        f();
        var a;
    }

}
```

### Correct: the rule accepts this

```js
function doSomething() {
    var first;
    var second; //multiple declarations are allowed at the top
    if (true) {
        first = true;
    }
}

function doSomethingElse() {
    var i;
    for (i=0; i<10; i++) {}
}
```

```js
var a;
f();
```

```js
class C {

    static {
        var a;
        if (something) {
            a = true;
        }
    }

    static {
        var a;
        f();
    }

}
```

Examples from the ESLint documentation.

## Enable it

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

```js
export default [
  {
    rules: {
      "vars-on-top": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

```json
{
  "rules": {
    "vars-on-top": "error"
  }
}
```

### Biome: `biome.json`

```json
{
  "linter": {
    "rules": {
      "style": {
        "useVarsOnTop": "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 Biome](https://lintradar.dev/presets/ultracite-biome.md) | Biome | `ultracite` | 616 | 2.1M | +151.7% |
| [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% |

## Related rules

- [eslint/grouped-accessor-pairs](https://lintradar.dev/rules/eslint/grouped-accessor-pairs.md): Require grouped accessor pairs in object literals and classes
- [eslint/prefer-object-spread](https://lintradar.dev/rules/eslint/prefer-object-spread.md): Disallow using `Object.assign` with an object literal as the first argument and prefer the use of object spread instead
- [eslint/no-promise-executor-return](https://lintradar.dev/rules/eslint/no-promise-executor-return.md): Disallow returning values from Promise executor functions
- [eslint/arrow-body-style](https://lintradar.dev/rules/eslint/arrow-body-style.md): Require braces around arrow function bodies
- [eslint/no-await-in-loop](https://lintradar.dev/rules/eslint/no-await-in-loop.md): Disallow `await` inside of loops
- [eslint/no-lonely-if](https://lintradar.dev/rules/eslint/no-lonely-if.md): Disallow `if` statements as the only statement in `else` blocks

---

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