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.
function demo() { if (true) { var inner = 1; // visible in the whole function } return inner; // 1 }
Try it
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.
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
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
varwith the same type is allowed. Redeclaring it with a different type is error TS2403 ("Subsequent variable declarations must have the same type"). - Reading a
varbefore its line givesundefinedat run time, not an error. TypeScript reports use-before-assign (TS2454, "Variable 'found' is used before being assigned") only when the type does not includeundefinedand it can prove the path. - At the top level of a module (a file with
importorexport) avaris local to the file. In a script file it becomes a global.
Usage notes
- Function scope. A
varinsideif,foror{}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
undefineduntil the assignment runs. - Shared loop variable. A
varin aforheader 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 xalso createsglobalThis.x;letandconstdo not. In Node ES modules this does not apply. declare var. This is the usual way to describe a global such asdeclare var process: NodeJS.Process. In a declaration file,declare varanddeclare letdiffer only in intent, butvaris the form that also adds a property toglobalThisin the type system.- Linting. The ESLint rule
no-varand the TypeScript compiler's emit for modern targets both assume you useletandconst.
Related
- let and const, function, declare, for
- Lessons: Primitive types, Type inference, Declaration files