JS CodeLab
basicsbeginner

JavaScript Variables (let, const, var) Code Example

•
Updated

Direct Answer & Overview

JavaScript variables are named memory containers used to hold and manipulate data values throughout runtime execution. Modern JavaScript uses const for fixed identifier bindings and let for mutable variables, enforcing block scope and eliminating var.

#variables#const#let#scoping

JavaScript Source Code

example.js
const name = "Suresh";
const age = 30;

console.log(name);
console.log(age);

How does this JavaScript code work?

Modern JavaScript distinguishes between constant references and reassignable bindings. The `const` keyword creates an immutable identifier binding within block scope; once assigned, you cannot rebind the identifier to another value. The `let` keyword declares a block-scoped variable that can be freely reassigned. Both `const` and `let` adhere to the Temporal Dead Zone (TDZ), meaning they cannot be accessed prior to their declaration line in the source code.

What is the expected terminal output?

Executing this code in JS CodeLab produces the following stdout sequence in the developer console:

Console Standard Output
Suresh
30

Common mistakes to avoid

  • !Attempting to reassign a variable declared with const, resulting in a TypeError: Assignment to constant variable.
  • !Using the legacy var keyword, which ignores block scopes (like if blocks and loops) and hoists uninitialized variables to undefined.
  • !Accessing a let or const variable before its declaration line, which triggers a ReferenceError due to the Temporal Dead Zone.
  • !Assuming const deeply freezes objects or arrays; const only prevents variable rebinding, not internal property mutations.

Frequently Asked Questions

When should I use const vs let in JavaScript?

Use const by default for all variable declarations to prevent accidental reassignment and improve code predictability. Use let exclusively when you know a value must be reassigned, such as loop counters or state flags.

Can you modify the properties of an object declared with const?

Yes. Declaring an object with const prevents reassigning the variable to a different object or memory reference, but its internal properties can still be modified, added, or deleted unless frozen with Object.freeze().

Why should developers avoid the var keyword in modern code?

The var keyword is function-scoped rather than block-scoped, leaking outside of if statements and loops. It also permits accidental variable re-declarations and hoists values as undefined, causing difficult-to-trace bugs.

What is the Temporal Dead Zone (TDZ)?

The Temporal Dead Zone is the period between entering a scope block and the point where a let or const variable is formally declared. Accessing the identifier during this window throws an immediate ReferenceError.

Comprehensive Tutorial

Want an in-depth understanding of this topic?

Read our complete, interactive tutorial covering memory management, edge cases, and best practices.

Read the full variables tutorial →