Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Keywords and Declarations

TypeScript var

Reference for var in TypeScript: function scope, hoisting, redeclaration, the loop-closure trap, global var versus globalThis, and why let and const are better.

var is the original JavaScript variable declaration. It is scoped to the nearest function (or to the module or script at the top level), not to the nearest block, and its declaration is hoisted to the top of that scope. TypeScript accepts var and infers its type like let, but modern code uses let and const instead.

You still meet var in older code, in compiled output for old targets, and in declare var statements that describe global variables in declaration files.

TypeScript
function demo() {
  if (true) {
    var inner = 1; // visible in the whole function
  }
  return inner;    // 1
}

Try it

TypeScript var ignores block scope

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript The loop closure trap

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
var name: Type = initializer;
var name = initializer;
var a = 1, b = 2;
declare var name: Type;   // ambient: describes a global, emits nothing
Part Description
name Identifier or destructuring pattern.
: Type Optional annotation; otherwise inferred, widened like let.
initializer Optional. Without it the variable starts as undefined.

Edge cases:

  • Redeclaring a var with the same type is allowed. Redeclaring it with a different type is error TS2403 ("Subsequent variable declarations must have the same type").
  • Reading a var before its line gives undefined at run time, not an error. TypeScript reports use-before-assign (TS2454, "Variable 'found' is used before being assigned") only when the type does not include undefined and it can prove the path.
  • At the top level of a module (a file with import or export) a var is local to the file. In a script file it becomes a global.

Usage notes

  • Function scope. A var inside if, for or {} leaks into the whole function. This is the main source of bugs and the reason to avoid it.
  • Hoisting. The declaration moves to the top of the scope, the assignment stays where it is. So the variable exists but holds undefined until the assignment runs.
  • Shared loop variable. A var in a for header is one variable for every iteration, so closures created in the loop all see its final value, as the second example shows.
  • Globals. In a non-module script, top-level var x also creates globalThis.x; let and const do not. In Node ES modules this does not apply.
  • declare var. This is the usual way to describe a global such as declare var process: NodeJS.Process. In a declaration file, declare var and declare let differ only in intent, but var is the form that also adds a property to globalThis in the type system.
  • Linting. The ESLint rule no-var and the TypeScript compiler's emit for modern targets both assume you use let and const.