close

no-var

Added in v0.4.0

Configuration

PresetConfigured Value
✅ ts.configs.recommended"error"
✅ ts.configs.recommendedTypeChecked"error"
✅ ts.configs.strict"error"
✅ ts.configs.strictTypeChecked"error"
✅ ts.configs.stylistic"error"
✅ ts.configs.stylisticTypeChecked"error"
rslint.config.ts
import { defineConfig, js } from '@rslint/core';

export default defineConfig([
  js.configs.recommended,
  {
    rules: {
      'no-var': 'error',
    },
  },
]);

Rule Details

Requires let or const instead of var. ECMAScript 6 introduced let and const as alternatives to var for variable declarations. let and const provide block scoping, which helps avoid common issues caused by the function scoping of var.

Examples of incorrect code for this rule:

var x = 'y';
var CONFIG = {};

Examples of correct code for this rule:

let x = 'y';
const CONFIG = {};

Original Documentation

Differences from ESLint

Rslint leaves some declarations unfixed when replacing var with let would introduce a temporal dead zone, even if ESLint offers a fix. This includes a function called during the variable's initialization that reads that variable:

var value = read();
function read() {
  return value;
}

The same protection applies to immediately invoked callbacks, constructors, getters and loop binding defaults that read an uninitialized variable. Stored callbacks and methods that run after initialization can still be fixed.