TypeScript follows the same rules as JavaScript for variable declarations. TypeScript variables can be declared using: var
, let
, and const
.
Var
Similar to JavaScript, we can declare variables in TypeScript using the var keyword. The scoping rules also remain the same as in JavaScript.
Let
The let declarations follow the same syntax as var declarations. Unlike variables declared with var
, variables declared with let
have a block-scope. This means that the scope of let variables is limited to their containing block.
Const
Variables can be declared using const similar to var or let declarations. The const makes a variable a constant where its value cannot be changed. Const variables have the same scoping rules as let variables.
TypeScript Var, Let, and Const Examples
var x = 10; let user = "DotNetCrunch"; const PI = 3.14;