- Home
- /
- What are Variables?
JavaScript Tutorial
IINTRODUCTION
JAVASCRIPT VARIABLES
JAVASCRIPT BASICS
JAVASCRIPT OBJECTS
DOM & BOM
OOPs
- Home
- /
- What are Variables?
What Are Variables in JavaScript?
In JavaScript, variables are like containers—they store data that your program can use, change, or pass around. Just like how you label a jar to know what’s inside it, variables let you name a value so you can refer to it later.
Without variables, programming would be chaotic. They allow us to store information such as numbers, text, or even more complex data like lists and objects.
Declaring Variables
JavaScript gives you three ways to declare variables:
1. var
The traditional way. It’s function-scoped and can be redeclared and updated.
var x = 10;
You can also declare it without a value:
var x;
2. let
Introduced in ES6, let
is block-scoped. You use this when the value might change later.
let score = 100;
score = 150; // works fine
3. const
Also introduced in ES6, const
is for values that should not be reassigned. Once set, it stays constant.
const pi = 3.14;
If you try to reassign it later, JavaScript will throw an error.
JavaScript Data Types
Variables can hold different kinds of data. Some of the most commonly used types are:
Numbers: Used for any kind of numeric value.
Example:let age = 25;
Strings: Sequences of text enclosed in quotes.
Example:let name = "Arshyan";
Booleans: True or false values, often used for conditions.
Example:let isOnline = true;
Arrays: Lists of values stored in a single variable.
Example:let fruits = ["apple", "banana", "mango"];
Objects: Collections of key-value pairs.
Example:
let person = {
name: "Arshyan",
age: 22
};