Day 1: JavaScript Basics โ Variables, Data Types & Operators ๐
๐ Welcome to the 30-Day JavaScript Learning Journey!
JavaScript is the backbone of web development and one of the most in-demand programming languages today. If you are just starting out or want to strengthen your JavaScript foundation, youโre in the right place!
This is Day 1 of our 30-day JavaScript learning series, where we will start from the very basics and gradually move to advanced topics.
Todayโs lesson is all about Variables, Data Types, and Operators โ the building blocks of JavaScript. By the end of this post, youโll be able to:
โ
Declare and use variables correctly
โ
Understand different types of data in JavaScript
โ
Use operators to perform calculations and logic
Letโs dive in! ๐
โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ
๐ What Are Variables in JavaScript?
==> A variable is like a labeled container that holds a value. Imagine you have a box where you store your favorite snacks. You can open the box, check whatโs inside, replace it with something else, or even lock it so nobody can change its contents.
In JavaScript, variables work in the same way. They store data that can be used and manipulated throughout the program.
๐ข How to Declare a Variable?
JavaScript provides three ways to declare a variable:
1๏ธโฃ var
โ The old way (not recommended)
2๏ธโฃ let
โ The modern and preferred way
3๏ธโฃ const
โ Used for constants (values that donโt change)
var name = "TestUser"; // Old way (avoid using this)
let age = 25; // Modern way (preferred)
const country = "India"; // Cannot be changed
๐น Key Differences: var
, let
, and const
๐น Best Practices for Using Variables:
โ
Use const
whenever possible (for values that donโt change).
โ
Use let
for variables that may change.
โ
Avoid var
due to scoping issues.
โ
Give meaningful variable names (e.g., userName
instead of x
).
โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ
๐ Understanding JavaScript Data Types
==> In JavaScript, data types define the kind of values a variable can hold. Since JavaScript is a loosely typed (or dynamically typed) language, a variable can hold different types of data without specifying the type explicitly.
๐น JavaScript Has Two Main Categories of Data Types
JavaScript data types are divided into two main categories:
1๏ธโฃ Primitive Data Types (Immutable, stored directly in memory)
2๏ธโฃ Non-Primitive (Reference) Data Types (Stored as references)
โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ
1๏ธโฃ Primitive Data Types (Hold a single value) : Primitive data types are immutable (cannot be changed after creation) and are stored directly in memory.
JavaScript has 7 primitive data types:
- ๐
String
-"Hello World"
- ๐ข
Number
-100
,3.14
- โ
Boolean
-true
,false
- โ
Undefined
- Variable declared but no value assigned - โซ
Null
- Intentionally empty - ๐ญ
Symbol
(Unique identifier) - ๐ข
BigInt
(For large numbers)
2๏ธโฃ Non-Primitive (Reference) Data Types (Hold complex values) : Non-primitive data types, also known as reference types, are objects and derived data types. They can store collections of values or more complex entities. The two key non-primitive data types in JavaScript are:
JavaScript has 3 primitive data types:
- ๐
Object
-{ name: "John", age: 30 }
- ๐ฆ
Array
-[1, 2, 3, 4]
- ๐ญ
Function
-function greet() { console.log("Hello!"); }
๐ Example of Data Types in Action
let city = "New York"; // String
let temperature = 25.5; // Number
let isRaining = false; // Boolean
let fruits = ["Apple", "Mango", "Banana"]; // Array
let person = { name: "John", age: 30 }; // Object
console.log(typeof city); // "string"
console.log(typeof temperature); // "number"
console.log(typeof fruits); // "object"
๐ Tip: Use typeof
to check the data type of any value!
โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ
๐ JavaScript Operators โ Performing Operations
Just like a calculator, JavaScript uses operators to perform calculations and comparisons.
1๏ธโฃ Arithmetic Operators ๐งฎ (Math Operations) : Used for basic calculations.
let a = 10, b = 5;
console.log(a + b); // 15 (Addition)
console.log(a - b); // 5 (Subtraction)
console.log(a * b); // 50 (Multiplication)
console.log(a / b); // 2 (Division)
console.log(a % b); // 0 (Remainder)
console.log(a ** b); // 100000 (Exponentiation)
2๏ธโฃ Assignment Operators ๐ (Assigning Values) : Used to assign values to variables.
let x = 10;
x += 5; // x = x + 5 (Now x = 15)
x -= 3; // x = x - 3 (Now x = 12)
x *= 2; // x = x * 2 (Now x = 24)
x /= 4; // x = x / 4 (Now x = 6)
3๏ธโฃ Comparison Operators ๐ (Checking Conditions) : Used to compare values.
console.log(10 == "10"); // true (Checks value only)
console.log(10 === "10"); // false (Checks value and type)
console.log(10 != 5); // true (Not equal)
console.log(10 > 5); // true (Greater than)
console.log(10 < 5); // false (Less than)
4๏ธโฃ Logical Operators ๐คฏ (Combining Conditions) : Used to make decisions.
let isSunny = true, isWarm = false;
console.log(isSunny && isWarm); // false (AND: both must be true)
console.log(isSunny || isWarm); // true (OR: one must be true)
console.log(!isSunny); // false (NOT: reverses boolean value)
โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ
๐ฏ Real-World Example โ Shopping Cart Calculation
Letโs put everything we learned into a simple shopping cart total price calculator:
let price = 500;
let discount = 10; // 10% discount
let finalPrice = price - (price * discount / 100);
console.log(`Final Price after Discount: $${finalPrice}`);
// Final Price after Discount: $450.
๐ Scenario: You have a product worth $500
, and a 10% discount applies. The final price is calculated as $450
.
โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ
๐ Conclusion
Today, we learned:
โ
How to declare variables using var
, let
, and const
โ
The difference between primitive and reference data types
โ
How to use arithmetic, assignment, comparison, and logical operators
๐น This knowledge is essential for writing JavaScript programs and will be the base for all future topics.
โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ
๐ Next Up: Day 2 โ JavaScript Functions & Scope
๐ฌ Did you learn something new today? Let me know in the comments!
๐ Follow me to stay updated for the next 29 days of JavaScript learning!