HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to JavaScriptVariables (let / const)Data TypesOperatorsStringsArraysObjectsConditions (if / else)Loops (for / while)FunctionsArrow Functions (=>)Array Methods (map / filter / reduce)
REFERENCE
📖 Overview
🖥️ Output & Console
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🗂️ Object
🔁 Condition & Loop
🧩 Function
🔄 Type & Conversion
JavaScript›Variables (let / const)

Variables (let / const)

Creating variables, constants with const, mutable variables with let, and naming rules.

Simple enough for a 5-year-old to understand

let and const

Values defined with const cannot be changed; those defined with let can. Both are like sticking a label on a box.

Example code
1
let counter = 0;
Mutable variable.
VariableMutable variable.
2
counter = 5;
We updated its value.
VariableWe updated its value.
3
const pi = 3.14;
Constant — cannot be changed.
VariableConstant — cannot be changed.
4
console.log(counter, pi);
5 3.14
Output / Input5 3.14
VariableOutput / Input
💡 Hint: Trying to reassign a value to a const throws an error. Use let if it's going to change.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Which one is used for a constant whose value will never change?

About this topic

There are three ways to declare variables in JavaScript: let, const, and var. This is due to the language's history — var is the old method, while let and const were added to the language in 2015. Which one you use is not a matter of taste; their behaviors differ, and the wrong choice leads to real bugs.

The rule of thumb is clear: use const by default. Use let if you need to change the value later. Never use var. You cannot reassign a value to a name defined with const; this is not a restriction, but a safeguard that makes it visible where and what the code changes.

What you will learn

  • The difference between let, const, and var
  • What block scope means
  • Why the content of an object defined with const can still change
  • Variable naming conventions and the camelCase convention
  • The difference between an undefined variable and null

When to use?

Use const if you will calculate a value once and then only read it. Use let for changing values such as a loop counter or an accumulated total.

const is also used for objects and arrays: the name doesn't change, but the content can. Defining an array with const and adding elements to it is completely valid and common practice.

Common mistakes

Wrong

const total = 0;
total = total + 5;

A name defined with const cannot be reassigned; you will get the error "TypeError: Assignment to constant variable". If the value is going to change, you must use let.

Correct

let total = 0;
total = total + 5;

Wrong

for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 100);
}

var does not respect block scope; all three timers share the same i variable and they all print 3. When let is used, each iteration gets its own copy of i and outputs 0, 1, 2. This is the most well-known trap of var.

Correct

for (let i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 100);
}

Wrong

console.log(name);
let name = "Ada";

A variable defined with let cannot be used before its definition; you will get "ReferenceError: Cannot access 'name' before initialization". The same code with var does not throw an error but returns undefined — which is a silent bug.

Correct

let name = "Ada";
console.log(name);

Frequently asked questions

Why am I able to add elements to an array I defined with const?

const fixes the value the name is bound to, not the contents of that value. An array is a reference; as long as the reference remains the same, you can add elements to it. If you try to completely replace the array with another array, you will get an error.

What is the difference between undefined and null?

undefined means "no value has been assigned at all" and JavaScript provides this itself. null means "there is intentionally no value here" and is assigned by the programmer. You see null when you want to empty a variable, and undefined when you haven't assigned anything.

Why is var still in the language?

For backward compatibility. Millions of lines of code written over the years use var, and browsers have to avoid breaking old sites. There is no reason to use it in new code; let and const are safer in every case.

Summary

const by default, let for values that will change, and never var. The fact that var doesn't respect block scope leads to surprising bugs, especially when using timers or event listeners inside loops.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code