
JavaScript Tutorial – Day 3: Variables and Data Types in JavaScript
Welcome back to our JavaScript tutorial series! 🚀Today, we will learn Variables and Data Types in JavaScript with clear examples.
So far:
- Day 1 – We introduced JavaScript and wrote our first program.
- Day 2 – We learned about adding JavaScript to HTML and structuring our code.
Now, on Day 3, we will cover variables and data types – the building blocks that allow us to store and work with data in JavaScript.
By the end of this blog, you’ll understand:
- What variables are and why we use them
- How to declare variables (
var
,let
,const
) - The different data types in JavaScript
What is a Variable?
A variable is like a container that stores data.
Think of it as a box with a label: you can put something inside (like a number or text), and later open the box to use it.
Example:
let name = "Anand";
let age = 25;
console.log(name); // Anand
console.log(age); // 25
Here, name
and age
are variables.
Declaring Variables in JavaScript
JavaScript gives us three ways to declare variables:
(a) var
(Old way)
var city = "Jaipur";
- Used in older JavaScript versions.
- Not recommended today because it has problems with scope.
(b) let
(Modern way)
let country = "India";
country = "USA"; // allowed
- Introduced in ES6 (2015).
- Can be reassigned.
- Preferred for variables that may change.
(c) const
(Constant values)
const pi = 3.14;
pi = 3.14159; // ❌ Error, cannot reassign
- Value cannot be changed after declaration.
- Best for values that should stay fixed (like configuration, constants).
Naming Rules for Variables
When naming variables, keep these rules in mind:
✔ Must start with a letter, _
, or $
✔ Cannot start with a number
✔ Case-sensitive (age
and Age
are different)
✔ Use meaningful names (not x
, y
, unless for short math codes)
✅ Good:
let userName = "Anand";
let totalMarks = 95;
❌ Bad:
let a = "Anand";
let b = 95;
Data Types in JavaScript
JavaScript has two categories of data types:
(A) Primitive Data Types
These are basic building blocks:
- String – Text data
let fruit = "Mango";
2. Number – Integers and decimals
let price = 99.99;
3. Boolean – True or False values
let isLoggedIn = true;
4. Undefined – A variable declared but not assigned a value
let x;
console.log(x); // undefined
5. Null – Represents “nothing” or empty value
let emptyBox = null;
6. Symbol – Unique values (advanced, used for special identifiers)
let id = Symbol("id");
7. BigInt – For very large numbers
let bigNumber = 1234567890123456789012345678901234567890n;
(B) Non-Primitive Data Type
- Object – Stores collections of data
let person = {
name: "Anand",
age: 25
};
console.log(person.name); // Anand
2. Array – Stores multiple values in a list
let colors = ["red", "green", "blue"];
console.log(colors[0]); // red
Mini Project – User Profile
Let’s combine variables and data types into a small project.
const name = "Anand";
let age = 25;
let isStudent = true;
let skills = ["JavaScript", "React", "Node.js"];
let userProfile = {
name: name,
age: age,
student: isStudent,
skills: skills
};
console.log(userProfile);
👉 Output:
{
name: "Anand",
age: 25,
student: true,
skills: ["JavaScript", "React", "Node.js"]
}
Here we used strings, numbers, booleans, arrays, and objects all together.
Key Takeaways
- Variables are containers for storing data.
- Use
let
andconst
(avoidvar
). - JavaScript has 7 primitive types (string, number, boolean, null, undefined, symbol, bigint) and objects/arrays for complex data.
- Always give meaningful names to variables.
What’s Next?
In Day 4 of our JavaScript tutorial series, we’ll explore Type Conversions & Operators. You’ll learn how to convert data from one type to another (e.g., string to number) and use operators to perform calculations.