Variables in JavaScript

Javascript 7 min min read Updated: Mar 09, 2026 Beginner
Variables in JavaScript
Beginner Topic 3 of 15

Variables in JavaScript

Variables are one of the most important concepts in programming. In JavaScript, a variable is used to store data that can be used later in a program. Instead of writing the same value repeatedly, developers store the value in a variable and reuse it whenever needed.

For example, if a program needs to store a user’s name, age, or score, it can store these values in variables. This makes the code easier to read, maintain, and update.

What is a Variable?

A variable is a named container used to store data. The value stored in a variable can change during the execution of a program.

Think of a variable like a labeled box. You put something inside the box and give the box a name. Whenever you need that item, you refer to the box by its name.

Declaring Variables in JavaScript

In JavaScript, variables can be declared using three keywords:

  • var
  • let
  • const

Each keyword is used to create variables, but they behave slightly differently.

Using var Keyword

The var keyword was traditionally used to declare variables in older versions of JavaScript.

javascript var name = "John"; console.log(name);

In this example, the variable name stores the value "John". When we print it using console.log(), the value is displayed in the console.

Using let Keyword

The let keyword was introduced in modern JavaScript (ES6). It is now commonly used for declaring variables because it provides better control over variable scope.

javascript let age = 25; console.log(age);

Here, the variable age stores the value 25. The value can be changed later in the program if needed.

Using const Keyword

The const keyword is used to declare variables whose values should not change after they are assigned.

javascript const country = "India"; console.log(country);

Once a value is assigned to a const variable, it cannot be reassigned.

Example of Changing Variable Values

Variables declared with let can be updated during program execution.

javascript let score = 10; score = 20; console.log(score);

Output:

output 20

Here the value of score was changed from 10 to 20.

Rules for Naming Variables

When creating variables in JavaScript, certain naming rules must be followed.

  • Variable names must begin with a letter, underscore (_), or dollar sign ($)
  • Variable names cannot start with a number
  • Spaces are not allowed in variable names
  • JavaScript keywords cannot be used as variable names
  • Variable names are case-sensitive

Example:

javascript let userName = "Alex"; let userAge = 30;

Best Practices for Variable Names

Good variable names make the code easier to understand. Developers should choose meaningful names that describe the purpose of the variable.

For example:

  • price instead of x
  • studentName instead of a
  • totalScore instead of num

Using descriptive names helps other developers understand the code more easily.

Conclusion

Variables are fundamental building blocks of JavaScript programming. They allow developers to store and manage data efficiently within a program. JavaScript provides three ways to declare variables: var, let, and const.

Modern JavaScript development mainly uses let and const because they provide better control and safer code structure. Understanding how variables work is essential before moving on to more advanced topics like functions, objects, and data manipulation.

In the next tutorial, you will learn about JavaScript Data Types and how different kinds of data are stored and used in variables.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators