🧠 JavaScript Variables & Data Types: The Building Blocks of Code
Before you build complex apps or master React, you’ve got to start with the basics. And nothing is more fundamental than variables and data types in JavaScript.
So buckle up — you're about to unlock the power of naming things and storing stuff in code (aka, becoming a real developer 😎).
📝 What Is a Variable?
A variable is like a labeled box where you store something. That “something” could be a number, some text, or even an entire list of items.
let name = "Dipank";
const age = 25;
var isDeveloper = true;
or you can also say that variables are named storage for data.In javascript we can use variables to store values like user data,session data ,cart item etc.
⚖️ var, let, and const — Which One Should You Use?
Here’s a quick cheat sheet:
Keyword | Can Reassign | Can Redeclare | Scope Type | Use When... |
---|---|---|---|---|
var |
✅ Yes | ✅ Yes | Function | Maintaining legacy code |
let |
✅ Yes | ❌ No | Block | Regular variable usage |
const |
❌ No | ❌ No | Block | Value won’t change |
💡 Tip: Use
const
when you can,let
when you must, and avoidvar
unless you're dealing with older projects.
🔍 JavaScript Data Types
In JavaScript, data comes in different forms called data types. There are two main categories:
🎯 Primitive Types (Stored by Value)
These types hold their value directly in memory:
- String — Words or text:
'Hello'
- Number — Numeric values:
30
,3.14
- Boolean —
true
orfalse
- Undefined — Declared but no value assigned
- Null — Intentionally empty
- Symbol — Unique identifiers
- BigInt — Extra-large numbers
const name = "JavaScript";
const isFun = true;
const balance = null;
🧠 Reference Types (Stored by Reference)
These types store references (or memory addresses) rather than actual data:
- Object — A collection of key-value pairs
- Array — Ordered list of values
- Function — Reusable blocks of code
let user = { name: "Aarav", age: 21 };
let colors = ["red", "green", "blue"];
let sayHi = function () {
console.log("Hi!");
};
🧪 typeof Operator — Inspect Like a Pro
Use typeof
to check what type of data a variable holds:
typeof 123; // "number"
typeof "Hi"; // "string"
typeof false; // "boolean"
typeof undefined; // "undefined"
typeof null; // "object" (quirk!)
typeof [1, 2]; // "object"
🔧 While debugging,
typeof
is your best friend.
🧬 Memory: Value vs Reference
Understanding how variables store data can save you hours of debugging:
- Primitive types: Stored by value, each variable gets its own copy.
- Reference types: Stored by reference, multiple variables can point to the same object.
let a = [1, 2];
let b = a;
b.push(3);
console.log(a); // [1, 2, 3] — because a and b share memory
🛑 Common Mistakes to Avoid
- Using
var
inside loops and expecting block scoping - Forgetting
const
can't be reassigned - Mixing up
undefined
(not assigned) andnull
(intentionally empty)
📚 Real-Life Analogy: Your Backpack
Imagine your code is a backpack. Each pocket (variable) holds something:
- Snacks (String)
- Coins (Number)
- A note saying “Open Later” (Undefined)
- An empty wrapper (Null)
- A secret symbol on the zipper (Symbol)
- A side pouch with tools (Object)
Every item is different, and how you store it matters!
📝 Summary: What You've Learned
- Variables store data you want to use in your programs.
- Choose
let
orconst
instead ofvar
in modern code. - Understand the difference between primitive and reference types.
- Use
typeof
to find out what kind of data you’re dealing with.
Practice these fundamentals, and you'll soon write JavaScript like a pro!
👉 Up Next: Operators & Expressions — let’s do some math, make comparisons, and write smarter logic!
✍️ Written with care by Simply Coder