# block-scoped-var

> Disallow using a `var` outside the block it is declared in

A `var` declared inside an `if`, a loop or a `switch` is visible in the whole function. Code after the block can still read it, and gets `undefined` if the block didn't run. This rule treats every `var` like a `let` and reports any use outside the block that declares it.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"block-scoped-var" = "error" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad */}
```js
function label(user) {
  if (user.admin) {
    var prefix = "Admin: ";
  }
  return prefix + user.name; // "undefinedAda" for everyone else
}
```

{/* bad */}
```js
function indexOfFirstEmpty(items) {
  for (var i = 0; i < items.length; i++) {
    if (!items[i]) break;
  }
  return i;
}
```

Not reported:

{/* good */}
```js
function label(user) {
  var prefix = "";
  if (user.admin) {
    prefix = "Admin: ";
  }
  return prefix + user.name;
}
```

## When not to use it

If `no-var` is on, there's no `var` left to check, and `let` and `const` already behave this way.
