Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

In JavaScript, a variable declaration creates a named binding; the data type belongs to the value currently held by that binding. Use const when you do not need to assign the binding again, let when you do, and learn var mainly to understand older code. JavaScript is dynamically typed, so a binding can hold values of different types over time.

What is a variable in JavaScript?

A variable is a binding between a name and a value. For example, let score = 10; declares the name score and initializes it with the number 10. The declaration is about how the name can be used; the type describes the value it currently refers to.

A declaration without an initializer, such as let count;, leaves the binding with the value undefined until a value is assigned. A const declaration must include an initializer.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How do let, const, and var differ?

The main practical differences are whether a binding can be reassigned, where it is visible, and whether it must be initialized immediately.

Declaration Scope Can the binding be reassigned? Initializer required?
let Block scope Yes No
const Block scope No Yes
var Enclosing variable environment; a block alone does not contain it Yes No

A block is code enclosed in braces, such as an if body or a loop body. let and const are visible only within the block where they are declared. The ECMAScript 2026 specification states that these declarations are scoped to the execution context’s lexical environment (ECMAScript declarations).

let score = 10;
score = 11; // reassignment is allowed

if (true) {
  let blockValue = "inside";
  var functionValue = "visible outside this block";
}

// blockValue is not available here
// functionValue is available here

A let or const binding also cannot be accessed before its initialization point; the period before initialization is called the temporal dead zone. var behaves differently because its scope is tied to the surrounding variable environment rather than limited to the nearest block. MDN notes that this behavior can be surprising and discourages using var in modern code (MDN: JavaScript language overview).

When should you choose each declaration?

  • Choose const when the name should keep referring to the same value. It is a good default when reassignment is unnecessary.
  • Choose let when your code needs to assign a new value to the binding.
  • Recognize var when reading existing code, especially when its visibility across a block matters.

Does const make an object immutable?

No. const prevents reassignment of the binding, but it does not freeze the object or array that the binding refers to. You can change an object’s properties while keeping the same binding:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const settings = { theme: "light" };
settings.theme = "dark"; // allowed

// settings = {}; // TypeError: reassignment is not allowed

This distinction is covered in MDN’s const reference.

What are JavaScript data types?

A data type is a category of value. ECMAScript defines seven primitive types and the Object type (ECMAScript 2024 language types).

  • undefined: commonly the value of a declared binding that has not been assigned another value.
  • null: an intentional absence of an object value.
  • boolean: either true or false.
  • number: ordinary numeric values.
  • bigint: integer values for use cases beyond ordinary Number values.
  • string: text.
  • symbol: a unique symbol value.
  • object: structured values. Objects and arrays are objects; functions are also objects in the language model.

The primitive-versus-object distinction is part of JavaScript’s language model. Arrays and functions may be treated as separate practical categories in everyday discussion, but they are objects in that classification.

Can a variable change its data type?

Yes. JavaScript is dynamically typed: a binding is not permanently restricted to the type of its first value. The type describes the value currently assigned to it.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
let value = 42;
console.log(typeof value); // "number"

value = "hello";
console.log(typeof value); // "string"

Here, the name value remains the same, but its current value—and therefore its type—changes. MDN explains this distinction in its JavaScript language overview.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How do you use typeof?

The typeof operator returns a string indicating the type category of a value. It is useful for inspecting simple values while learning:

typeof 500;      // "number"
typeof "500";    // "string"
typeof true;     // "boolean"
typeof undefined; // "undefined"

Quotation marks matter: "500" is text, not a number. typeof inspects the value it receives; it does not reveal a permanent type attached to the variable name. MDN’s beginner guide demonstrates this use of typeof (MDN: Variables).

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.