JavaScript Variables and Data Types – Beginner’s Complete Guide
When you start learning JavaScript, the first thing you must understand is how to store and manage data. This is where variables and data types come into play. They are the foundation of every JavaScript program.
In this guide, you’ll learn everything about JavaScript variables, var, let, and const, along with all data types in JavaScript — both primitive and non-primitive.
🔹 What Are Variables in JavaScript?
A variable is like a container that stores data. You can assign, change, or access the stored value anytime during your program.
let name = "Lokesh"; let age = 25; let isDeveloper = true;
Here, name, age, and isDeveloper are variables that hold string, number, and boolean data respectively.
🔸 Declaring Variables: var, let, and const
JavaScript allows you to declare variables in three ways — using var, let, or const.
| Keyword | Scope | Reassignable | Best Use |
var | Function Scoped | ✅ Yes | Old style, avoid using |
let | Block Scoped | ✅ Yes | Use for changeable values |
const | Block Scoped | ❌ No | Use for fixed values |
var city = "Jaipur"; let country = "India"; const PI = 3.14159;
🔹 Rules for Naming Variables
- Names must start with a letter, underscore (_) or dollar sign ($)
- Cannot start with a number (❌ 1name)
- Case sensitive —
age and Age are different - Use meaningful names like
userName or totalAmount
🔸 JavaScript Data Types
Data types define the kind of values a variable can hold. JavaScript has two main categories of data types:
1️⃣ Primitive Data Types
Type Example Description
String "Lokesh" Textual data
Number 25 Numeric value
Boolean true True/false logic
Undefined let x; Declared but no value assigned
Null let y = null; Empty or intentional absence of value
Symbol Symbol("id") Unique identifier
BigInt 12345678901234567890n Large integer values
let name = "Lokesh"; let age = 25; let isActive = true; let salary = null; let id = Symbol("userId"); let bigNumber = 12345678901234567890n;
2️⃣ Non-Primitive (Reference) Data Types
Type Example Description
Object { name: "Lokesh", age: 25 } Key-value pairs
Array [10, 20, 30] Ordered list
Function function greet() {} Block of reusable code
let person = { name: "Lokesh", age: 25 }; let colors = ["red", "green", "blue"]; function greet() { console.log("Hello, world!"); }
🔍 Checking Data Type with typeof Operator
You can easily find out what type of data a variable holds using the typeof keyword.
console.log(typeof "Lokesh"); // string console.log(typeof 25); // number console.log(typeof true); // boolean console.log(typeof {}); // object console.log(typeof []); // object console.log(typeof null); // object
💡 Common Mistakes to Avoid
- ❌ Using
var everywhere — use let and const instead - ❌ Confusing
null and undefined - ❌ Reassigning
const variables - ✅ Use
typeof to debug your data types
🚀 Real-World Example
const user = { name: "Lokesh", age: 25, isPremium: false }; if (user.isPremium) { console.log("Welcome, Premium User!"); } else { console.log("Upgrade to Premium for more features!"); }
✅ Output: Upgrade to Premium for more features!
📚 Quick Summary
- Variables store values in memory.
- Use let for changeable values and const for constants.
- JavaScript has 7 primitive and 3 non-primitive data types.
typeof helps you identify the data type.
🎯 SEO Tip:
If you’re searching for “difference between var let const in JavaScript” or “JavaScript data types with examples,” this guide covers all the essential explanations and examples for beginners.
📚 FAQ (Frequently Asked Questions)
Q1: What is the difference between var, let, and const?
var is function-scoped, while let and const are block-scoped. Use let when the value changes and const when it doesn’t.
Q2: How many data types are there in JavaScript?
There are 7 primitive and 3 non-primitive (object, array, function) data types in JavaScript.
Q3: Is null an object in JavaScript?
Technically, typeof null returns “object”, but it’s a long-standing JavaScript quirk.
Q4: What is the difference between undefined and null?
undefined means a variable has been declared but not assigned a value, while null means an intentional empty value.
➡️ Next Lesson:
In the next part, we’ll learn about JavaScript Operators — arithmetic, comparison, logical, and assignment operators with examples.
Keep coding and stay consistent 💪