Understanding Variables and Data Types in JavaScript
When you start to learn JavaScript, the first thing that you will be introduced to is variables and data types. These are the essentials of a programming language. Having a clear understanding of these concepts, the rest will follow.
Let us understand these concepts stepwise in a simple way.
What Are Variables and Why Do We Need Them?
Think of a variable like a box that stores information.
The name of the box → variable name
The content inside the box → value
Real-life example:
If you want to store your name:
let name = "Gunjan";
Here:
name→ variable"Gunjan"→ value stored inside it
Why do we need variables?
To store data
To reuse values
To make programs dynamic and flexible
How to Declare Variables in JavaScript
JavaScript provides three ways to declare variables:
1. var (Old way)
var age = 20;
2. let (Modern and preferred)
let age = 20;
3. const (For fixed values)
const pi = 3.14;
Key Difference Between var, let, and const
Feature | var | let | const |
|---|---|---|---|
Re-declaration | Allowed | Not allowed | Not allowed |
Re-assignment | Allowed | Allowed | Not allowed |
Scope | Function | Block | Block |
Example:
let age = 20;
age = 25; // allowed
const pi = 3.14;
pi = 3.15; // error
Rule of thumb:
Use
letwhen value may changeUse
constwhen value should not changeAvoid
varin modern JavaScript
Primitive Data Types in JavaScript (In Depth)
Primitive data types are the most basic building blocks in JavaScript. They store single values and are immutable.
Immutable means once created, its value cannot be changed directly (JavaScript creates a new value instead).
🔹 1. String (Text)
Strings represent text.
let name = "Gunjan";
let greeting = 'Hello';
let message = `Hi ${name}`; // template literal
Key Points:
Can use
" ",' ', or`Template literals allow variables inside strings
Strings are immutable
let str = "hello";
str[0] = "H"; //won't change
console.log(str); // "hello"
🔹 2. Number
JavaScript has only one number type (no int/float separation).
let age = 20;
let price = 99.99;
Special cases:
console.log(1 / 0); // Infinity
console.log("abc" * 2); // NaN
Important concept:
Stored as 64-bit floating point
A precision issue exists:
console.log(0.1 + 0.2); // 0.30000000000000004
🔹 3. Boolean (true/false)
Used for decision-making.
let isStudent = true;
let isLoggedIn = false;
Used in conditions:
if (isStudent) {
console.log("You are a student");
}
Truthy vs Falsy:
Falsy values:
false
0
""
null
undefined
NaN
Everything else is truthy
4. Undefined
A variable declared but not assigned gets undefined.
let x;
console.log(x); // undefined
Assigned automatically by JavaScript
🔹 5. Null
Represents an intentional empty value.
let data = null;
Difference:
undefined | null |
|---|---|
JS assigns | Developer assigns |
let a;
let b = null;
console.log(a); // undefined
console.log(b); // null
6. Symbol (Advanced)
Used to create unique identifiers.
let id1 = Symbol("id");
let id2 = Symbol("id");
console.log(id1 === id2); // false
Always unique—even with the same description
7. BigInt
Used for very large numbers beyond the safe limit.
let big = 123456789012345678901234567890n;
What is Scope? (Simple Explanation)
Scope defines where a variable can be accessed.
Block Scope (let, const)
{
let x = 10;
console.log(x); // works here
}
console.log(x); // error
Function Scope (var)
function test() {
var y = 20;
}
console.log(y); // error
In simple terms:
letandconststay inside.{ }varstays inside a function
Practical Example
Let’s put everything together:
let name = "Gunjan";
let age = 20;
let isStudent = true;
console.log(name);
console.log(age);
console.log(isStudent);
// Changing values
age = 21;
console.log(age);
// const example
const country = "India";
// country = "USA"; error
Mini Assignment (Try Yourself)
Declare variables for:
Name
Age
IsStudent
Print them in the console
Try:
Changing values of
letChanging values of
const(observe error)
Final Thoughts
Variables and data types are the foundation of JavaScript. If you get comfortable with:
How to store data
When to use
letvsconstBasic data types
Scope
You’ll be ahead of most beginners.
Don’t just read—write code and experiment. That’s where real learning happens.