All Articles

const, let, var in TypeScript

There are three options to declare variables in TypeScript:

  • const - block-scoped, the value can’t be reassignment or redeclared. Should be initialized. This is a read-only reference for value. You can’t reassign the object for the constant, but you can change properties for the object.
  • let - block-scoped, can be not initialized to a value.
  • var - declares a function-scoped or globally-scoped variable. It can provide side effects (hoisting, re-declaration, shadowing, block-scoped variable capturing) in your codebase.

GOOD PRACTICE:

  • Use const for all declarations, that you don’t plan to modify.
  • Use let instead of var for most cases.
  • Avoid var.