redline
LowType safetyvar-declaration

`var`

`var`. var is function-scoped and hoisted. Use const or let.

This is how the comment reads inline, under the flagged line.

Why it matters

var leaks out of blocks and can be used before it's assigned, which causes subtle loop and closure bugs.

What to do

const by default, let when it's reassigned.

Examples

Flagged
var total = 0;
Better
let total = 0;

What it catches

It reads a pattern on each line, aware of strings and comments. Languages: TypeScript and JavaScript. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.

Flagged

var a = 1;

Left alone

const a = 1;
// var a

Research

`==`, `var`, `== None`, and `is` with a literal. Coercion and leaky scope make comparisons true or false for the wrong reason.

Turn it off or change it

In a config file for the CLI, or in the API's rules field:

{
  "rules": {
    "disable": [
      "var-declaration"
    ],
    "severity": {
      "var-declaration": "medium"
    }
  }
}

Read the source or report a false positive.