Replace var with let or const in JavaScript

Paste older JavaScript and every var declaration becomes let or const. In smart mode, names that are never reassigned get const, loop counters and reassigned names get let.

Live demo · real result from this tool
Replace var with

Converted code

let total = 0;const items = getItems();for (let i = 0; i < items.length; i++) {  total += items[i].price;}const label = "var stays inside strings";
var replaced4
Became const
2
Became let
2

var is function-scoped and hoisted; let/const are block-scoped and cannot be used before their declaration. Review code that uses a variable before it is declared or outside the block it was declared in.

Processed on your device — nothing is uploaded.

How to use the var to let / const converter

  1. Paste your code into the JavaScript box.
  2. Choose Replace var with: const if never reassigned, else let (smart, the default), let, or const.
  3. Copy the Converted code.
  4. Check the stats (var replaced, Became const, Became let) and read the warnings under the result.

What it does and when to use it

Since ES2015, let and const are the normal way to declare variables, and most style guides ban var. When you update an old script, a tutorial snippet or a legacy file before refactoring it, this tool makes the change in one pass and shows which names are reassigned.

To go the other way for old browsers, use the ES6 to ES5 converter. For other clean-ups, the ternary to if/else converter expands dense conditions and the JavaScript formatter tidies layout. Check the final code with the JavaScript validator.

How it works

The code is tokenized, so var inside a string, a comment, a regex or a property such as obj.var is never changed. The tool then counts, for every name, how often it is written: plain and compound assignments such as += or ??=, and ++ or --. Smart mode chooses const for a declaration only when all of these hold:

  • every name in it has a starting value
  • no name is written again after its declaration
  • the name is declared with var only once
  • it is not the counter of a classic for (…; …; …) loop

Otherwise it uses let. A for…in or for…of loop variable can be const, because each pass gets a fresh binding. In const mode every var becomes const, and each one that would fail is listed by line.

Worked examples

Smart mode on the sample.

let total = 0;
const items = getItems();
for (let i = 0; i < items.length; i++) {
  total += items[i].price;
}
const label = "var stays inside strings";

Four declarations were replaced: 2 became const and 2 became let. total is reassigned with += and i is a loop counter. The word “var” inside the string is untouched.

Forcing const. For var x = 1; var x = 2; var y; y = 3; on separate lines, const mode reports “Line 3: y has no initial value — const will throw there; use let.” and warns that x is declared with var 2 times.

Property and string named var. var s = 'var'; obj.var = 1; in let mode becomes let s = 'var'; obj.var = 1;.

Limits and tips

  • Hoisting is not analysed. A warning reminds you to look for code that uses a variable before its declaration.
  • Closures in loops change, for the better. With let, each loop pass gets its own i, which fixes the classic callback bug but can change output in code that relied on the shared value.
  • Destructuring is handled. var [c, d] = arr; becomes let if d is reassigned later, and const otherwise.

Frequently asked questions

Is it safe to replace var with let everywhere?
Usually, but not always. var is visible in the whole function, even before its line; let and const exist only in their block and only after the declaration. Code that relies on the old behaviour throws a ReferenceError. The tool warns about this on every run.
Why did an object I change become const?
const stops the name from being pointed at a new value, not the value from changing. cfg.debug = true and items.push(x) still work on a const object, so smart mode keeps const when only properties change.
What happens when the same name is declared twice?
let and const cannot be redeclared in one scope. The tool gives that name let in smart mode and warns how many times it was declared, so you can rename or remove the extra declaration.
Does it understand separate functions?
Not fully. It counts assignments across the whole input, so two functions that both use a variable called i are treated as one name. Check the result in larger files, or use ESLint's no-var and prefer-const rules with autofix.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close