JavaScript Variables and Constants (With Examples)
45 mins read
Last updated: 16 Feb 2026
8478 views
Share
6.78%
Table of Contents
Introduc on
In JavaScript, variables and constants are used to store data that we can use and modify later
in our program. They act as containers that hold values such as numbers, strings, or objects.
We use variables when the value can change during program execu on, while constants
store fixed values that cannot be reassigned once defined. Understanding how to declare
and use them helps us write cleaner, more efficient, and bug-free code.
Example
let name = "Riya";
const PI = 3.14159;
[Link]("Name:", name);
[Link]("Value of PI:", PI);
Run Code
Output
Name: Riya
Value of PI: 3.14159
Here, we used let to declare a variable name whose value can be changed later, and const
for PI, which stays constant throughout the program.
What is a JavaScript Variable?
A JavaScript variable is a container used to store data that we can use and modify later in a
program. It allows us to give a name to a value, making our code easier to read and manage.
Variables can hold different types of data, such as numbers, strings, or objects. There are
two main types of variables in JavaScript: Local variables and Global variables, which we will
explore later in this chapter.
Declaring a Variable in JavaScript
Before using a variable in a program, we must declare it. In JavaScript, variables are
dynamically typed, which means the type of data stored is determined at run me. So, we
don’t need to define the data type explicitly.
Rules for Declaring a Variable in JS
JavaScript variables are case-sensi ve (for example, WORLD and world are different).
A variable name cannot be a reserved keyword (like var, if, func on, etc.).
Variable names must start with a le er, $ (dollar sign), or _ (underscore).
A er the first character, names can also contain digits (0–9), but cannot start with a
number.
JavaScript Variable Declara on Methods
We can declare variables using three keywords: var, let, and const.
var – Used in older JavaScript; func on-scoped and can be redeclared.
let – Block-scoped and can be updated but not redeclared in the same scope.
const – Block-scoped and cannot be changed once assigned.
Syntax
var variableName = value;
let variableName = value;
const variableName = value;
Example
var name = "Riya";
let age = 25;
const country = "India";
[Link](name);
[Link](age);
[Link](country);
Run Code
Output
Riya
25
India
Here, we declared variables using var, let, and const. The values are printed using the
JavaScript [Link]() method. The const variable’s value cannot be changed later, while
var and let can be reassigned.
Difference Between var, let, and const in JavaScript
Let’s compare let, var, and const keywords in JavaScript based on different proper es:
Property var let const
Scope Func on scoped Block scoped Block scoped
The old version of the The old version of the browser The old version of the brows
Support
browser supports it. doesn’t support it. doesn’t support it.
Hois ng Hoisted with undefined Hoisted but not ini alized Hoisted but not ini alized
Redeclara on Can be redeclared Can’t be redeclared Can’t be redeclared
Upda on Mutable (can be updated) Mutable (can be updated) Immutable (cannot be upda
Origin Pre ES2015 ES2015(ES6) ES2015(ES6)
Example
var city = "Delhi";
let age = 25;
const country = "India";
city = "Mumbai"; // allowed
age = 26; // allowed
// country = "USA"; // Error: Assignment to constant variable
[Link](city);
[Link](age);
[Link](country);
Run Code
Output
Mumbai
26
India
Here, we changed values of var and let variables successfully. However, reassigning a const
variable causes an error because constants cannot be updated once defined.
Ini alizing a Variable in JavaScript
Ini aliza on means assigning a value to a JavaScript variable. We can do this at the me of
declara on or later in the program when needed. Since JavaScript is an untyped language, a
variable can store any data type without specifying its type during declara on.
We can even change a variable’s data type during execu on, and JavaScript will handle it
automa cally. To assign a value, we use the assignment operator (=).
Example 1:
// Declare variable num
let num;
// Assign 9 to num
num = 9;
Here, we first declare the variable num and then assign it the value 9.
Example 2:
// Declare variable num1 and assign 9 to it
let num1 = 9;
// Declare variable num2 and assign 6 to it
let num2 = 6;
Here, we ini alize variables num1 and num2 during declara on for cleaner and shorter
code.
Changing the Value of a JavaScript Variable
In JavaScript, we can change the value of a variable any me during the program. This
flexibility is why it’s called a variable. When we assign a new value, the old one gets replaced
automa cally.
Example
let language = "JavaScript";
// Change the value
language = "Python";
[Link](language); // Output: Python
Run Code
Output
Python
Here, we first assign the value "JavaScript" to the variable language. Later, we change it to
"Python", and the updated value is displayed in the console.
Using Variables with Different Data Types in JavaScript
In JavaScript, variables can store different types of data. Each value has a data type, which
defines the kind of data stored in the variable. JavaScript is dynamically typed, meaning we
don’t need to specify the type; it is determined automa cally.
The main data types in JavaScript are:
String – Text values wri en in quotes ("Hello")
Number – Numeric values (25, 3.14)
Boolean – True or false values (true, false)
Object – Collec on of key-value pairs ({ name: "Riya", age: 22 })
Array – List of values ([1, 2, 3])
Undefined – Variable declared but not assigned a value
Null – Represents an empty or non-existent value
Example
let name = "Riya"; // String
let age = 22; // Number
let isStudent = true; // Boolean
let hobbies = ["Reading", "Traveling"]; // Array
let person = { city: "Jaipur", country: "India" }; // Object
[Link](name);
[Link](age);
[Link](isStudent);
[Link](hobbies);
[Link](person);
Run Code
Output
Riya
22
true
["Reading", "Traveling"]
{ city: "Jaipur", country: "India" }
Here, we use variables to store different data types such as JavaScript strings, numbers,
booleans, arrays, and objects. Each type helps represent different kinds of informa on in our
program.
JavaScript Variable as Iden fiers
In JavaScript, a variable acts as an iden fier, meaning it is the name we give to a memory
loca on where data is stored. Iden fiers help us refer to variables, func ons, or objects
easily in our code.
Rules for Iden fiers:
Iden fiers can include le ers, digits, underscores (_), and dollar signs ($).
They must not start with a digit.
JavaScript iden fiers are case-sensi ve.
We can’t use JavaScript reserved keywords as iden fiers.
Example
let firstName = "Riya"; // valid
let _age = 22; // valid
let $city = "Jaipur"; // valid
// let 1country = "India"; // invalid (starts with a number)
[Link](firstName);
[Link](_age);
[Link]($city);
Run Code
Output
Amit 450 A
Here, we use variables as iden fiers studentName, _totalMarks, and $grade. Each stores a
value that we can access and print easily in our program.
JavaScript Variable Scope
The scope of a variable in JavaScript refers to the area of the program in which it is defined.
Variables have two scopes:
1. Global Variables
A global variable can be defined anywhere in the code and is accessible from any func on. It
can be declared outside the func on or with the window object.
Example:
var globalVar = "I am a global variable";
func on displayGlobalVar() {
[Link](globalVar); // Accessible here
displayGlobalVar();
Run Code
Output:
I am a global variable
2. Local Variables
A local variable can be declared within a block or func on and is accessible only within that
scope. Func on parameters are local to the func on. In a func on body, a local variable
takes precedence over a global variable with the same name, effec vely hiding the global
variable.
Example:
func on showMessage() {
let message = "Hello, I am a local variable!";
[Link](message);
showMessage();
[Link](message);
Run Code
Output:
Hello, I am a local variable!
Error: message is not defined
What is a JavaScript Constant?
In JavaScript, the const keyword is used to declare variables whose values cannot be
changed a er assignment. It helps us create constants that remain fixed throughout the
program.
The const keyword was introduced in ES6 to define immutable variables. Once a value is
assigned to a const variable, it cannot be redeclared or reassigned within the same scope.
Constants also have block scope, just like variables declared with let.
Syntax
const constantName = value;
Features of Values Defined Using const in JavaScript
A variable declared with const must be ini alized at the me of declara on.
The value of a const variable cannot be reassigned once defined.
const provides block scope, meaning it’s only accessible within the block where it’s
declared.
While the variable reference is constant, object or array values inside it can s ll be
modified.
Declaring Constants in JavaScript
If a variable is declared using the const keyword, we must assign a value to it at the me of
declara on. We can’t declare a variable with the const keyword without ini aliza on.
Example
const num;
num = 10;
[Link](num);
Run Code
Output
SyntaxError: Missing ini alizer in const declara on
So, how to declare a const variable in the correct way:
const num = 10;
[Link](num); // 10
Examples of const in JavaScript
Here are some simple examples of using const in JavaScript:
1. Cannot be Reassigned
const does not allow reassignment or redeclara on. Once a value is assigned to a const
variable, it cannot be changed.
Example:
const z = 10;
z = 20; // Error: Cannot reassign a constant variable
Run Code
Output:
TypeError: Assignment to constant variable
2. Block Scope
A variable in JS declared using the const keyword has the Block scope, so one variable is
treated differently inside and outside the block. We can’t redeclare the const keyword within
the same block.
Example:
const blockScoped = "Inside Block";
[Link](blockScoped);
[Link](blockScoped);
Run Code
Output:
Inside Block
Error: blockScoped is not defined
3. Cannot be Hoisted
Variables defined with const are not hoisted to the top of the code and cannot be accessed
before ini aliza on.
Example:
[Link](a); // Error: Cannot access 'a' before ini aliza on
const a = 5;
[Link](a);
Run Code
Output:
Error: Cannot access 'a' before ini aliza on
4. const in Arrays
Variables declared using the const keyword have a constant reference but not a constant
value. Therefore, we can update the same array declared with the const keyword, but can’t
change the reference to the new array to the constant variable.
Example:
const numbers = [1, 2, 3];
[Link](4); // Modifying the content
[Link](numbers);
numbers = [5, 6];
Run Code
Output:
[1, 2, 3, 4]
Error: Assignment to constant variable
5. const in Objects
The object proper es can be changed, but reference to the object can’t be changed.
Example:
const person = { name: "Alice", age: 25 };
[Link] = 26; // Modifying a property
[Link](person);
person = { name: "Alex" };
Run Code
Output:
{ name: "Alice", age: 26 }
Error: Assignment to constant variable