How to Declare a Variable in JavaScript

What you’ll build or solve

You’ll declare variables that store values correctly and follow modern JavaScript best practices.

When this approach works best

Use this approach when you:

  • Store user input, configuration values, or calculation results.
  • Track state inside loops or conditionals.
  • Define constants like API URLs or tax rates.

Prefer modern syntax in new projects. Avoid var unless maintaining older codebases.

Prerequisites

  • A .js file or an HTML file with a <script> tag
  • Basic knowledge of strings, numbers, and arrays

No additional setup is required.


Step-by-step instructions

1) Choose the right declaration keyword

JavaScript has three keywords for declaring variables. Pick the one that matches your intent.

Option A: Use const by default

Use const for values that should not be reassigned.

const taxRate = 0.2;
const name = "Alex";

Reassignment causes an error:

const taxRate = 0.2;
// taxRate = 0.3; // Error

Use const unless you know the value must change.


Option B: Use let when reassignment is required

Use let if the variable’s value will change later.

let score = 10;
score = 20;

You can also declare first and assign later:

let total;
total = 100;

Switch to let only when reassignment is needed.


Option C: Use var only for legacy code

var works differently from let and const.

var count = 5;

Avoid var in modern development because it does not respect block scope.


What to look for

Declare and assign in one line:

const user = "Sam";

Or declare first, assign later:

let result;
result = 42;

let and const are block-scoped:

if (true) {
  const message = "Hello";
  console.log(message);
}

// console.log(message); // Error

var ignores block scope:

if (true) {
  var test = "Visible outside";
}

console.log(test);

You cannot redeclare let or const in the same scope:

let price = 100;
// let price = 200; // Error

const prevents reassignment, not mutation:

const user = { name: "Sam" };
user.name = "Alex"; // Allowed

Declare variables before using them to avoid temporal dead zone errors:

let total = 50;
console.log(total);

Examples you can copy

Example 1: Counter that changes

let counter = 0;

counter += 1;
counter += 1;

console.log(counter);

Use let because the value updates.


Example 2: Configuration value

const API_URL = "https://api.example.com";

console.log("Fetching from:", API_URL);

Use const for values that should stay fixed.


Example 3: Loop variable

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

// console.log(i); // Error

If you use var instead:

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

console.log(i); // Accessible here

This difference affects how your code behaves.


Common mistakes and how to fix them

Mistake 1: Reassigning a const variable

What you might do:

const rate = 0.2;
rate = 0.3;

Why it breaks: const variables cannot be reassigned.

Fix:

let rate = 0.2;
rate = 0.3;

Mistake 2: Using var inside blocks

What you might do:

if (true) {
  var message = "Hi";
}

Why it breaks: var ignores block scope and may leak outside the block.

Fix:

if (true) {
  let message = "Hi";
}

Mistake 3: Accessing before declaration

What you might do:

console.log(total);
let total = 50;

Why it breaks: let and const cannot be used before declaration.

Fix:

let total = 50;
console.log(total);

Troubleshooting

If you see “Assignment to constant variable,” change const to let.

If you see “Cannot access before initialization,” move the declaration above usage.

If a variable is accessible outside a block unexpectedly, check for var.

If changes do not appear in the browser, refresh the page.

If Node shows errors, confirm you are running the correct file.


Quick recap

  • Use const by default.
  • Use let when reassignment is needed.
  • Avoid var in modern code.
  • Remember block scope rules.
  • Declare variables before using them.