Variables

Beginner·15 min read·Lesson 1 of 8·Not Started

Variables are the fundamental building blocks of JavaScript. They store data that can be referenced and manipulated throughout your code.

var, let, and const

Introduced in ES6, let and const provide block-scoped variable declarations, addressing many issues with function-scoped var.

var is function-scoped and hoisted to the top of its function. let and const are block-scoped and not initialized until their declaration is evaluated — this is called the temporal dead zone.

Key Differences

  • var: function-scoped, can be redeclared, hoisted with undefined initialization
  • let: block-scoped, cannot be redeclared in same scope, hoisted but not initialized (TDZ)
  • const: block-scoped, cannot be reassigned, must be initialized at declaration

Best Practices

Prefer const by default. Use let when you need to reassign. Avoid var in modern code.