Back to JavaScript Notes
Topic #276

JavaScript Variables

By the end, you can store values in JavaScript variables using let and const, assign and reassign values, and choose the right declaration for data that changes or stays fixed.

What it is

A JavaScript variable is a named binding that connects an identifier, such as name, to a value, such as "Ada". A useful mental model is a label attached to a value: the label can be moved to a new value when you reassign it, but the label itself is not the value.

Declaration creates the binding. Assignment stores a value in it. For primitive values like strings and numbers, the binding holds the value. For objects and arrays, the binding holds a reference to the object. Related terms include identifier, binding, scope, reassignment, and temporal dead zone, the period before a let or const declaration is reached.

Why it matters

  • Variables store user input, such as a name entered in a form.
  • They track changing state, such as a score, timer, or selected item.
  • They make code readable by giving values meaningful names.
  • They let you pass data between functions and reuse calculations.
  • Using const helps prevent accidental reassignment.

Syntax or steps

  1. Choose a declaration keyword: const if the binding will not be reassigned, let if it will change.
  2. Write a descriptive identifier, such as userName or totalPrice.
  3. Use = to assign an initial value.
  4. Reassign only variables declared with let.
const name = "Ada";
let age = 36;
age = age + 1;

Example

const greeting = "Hello";
let name = "Ada";
let message = greeting + ", " + name + "!";

console.log(message);

name = "Grace";
message = greeting + ", " + name + "!";

console.log(message);
  • const greeting = "Hello"; creates a binding that cannot be reassigned.
  • let name = "Ada"; creates a binding that can be reassigned.
  • let message = greeting + ", " + name + "!"; evaluates the right side first, then stores the resulting string in message.
  • name = "Grace"; changes the value associated with name.
  • The second console.log shows the updated message.

Common mistakes

  • Using const for a value that changes. This causes a TypeError. Use let when reassignment is needed.
  • Forgetting to declare a variable. This can create accidental globals in non-strict code or throw a ReferenceError in strict mode. Always declare with let or const.
  • Using var by habit. var has function scope and hoisting behavior that can cause surprises. Prefer let and const.
  • Assuming const freezes objects. const prevents reassignment, not mutation. You can still change object properties unless you freeze the object.

When to use it

Variables are the basic way to store data. The closest choice is which declaration keyword to use.

KeywordReassignmentScopeBest for
constNoBlockValues that should not be reassigned
letYesBlockValues that change over time
varYesFunctionLegacy code only

A good default is const. Switch to let only when the binding must change.

Practice

Guided exercise: create a constant app name, a variable for completed tasks, increase it, and log both values.

const appName = "Study Tracker";
let tasksCompleted = 0;
tasksCompleted = tasksCompleted + 3;
console.log(appName, tasksCompleted);

Expected output: Study Tracker 3.

Challenge: store a price in a const, store a discount in a let, calculate the final price, and log it. Hint: use let only if the discount changes.

Quick check

Question: Which declaration should you use for a variable whose value will change later, and why?

Answer: Use let, because it allows reassignment within its block scope. const creates a binding that cannot be reassigned.

Summary

JavaScript variables are named bindings that store values or references. Assignment evaluates a value and attaches it to a binding, while reassignment changes that binding.

Prefer const for fixed bindings and let for changing ones, and avoid var in modern code.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

JavaScript Variables – FAQs

Quick answers about learning JavaScript Variables in JavaScript.

This free note from Coding Now Tech Institute explains JavaScript Variables in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including JavaScript Variables, is 100% free with no signup required.
With focused practice, most students grasp JavaScript Variables in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now