🚀 JavaScript Series Part 3: Mastering Variables, Data Types & Operators Like a Pro!

Table of contents
- Welcome, welcome:
- 🌟 Magic Trick: Dynamic Greeting! 🚀
- 🏺 Variables: Your Storage Containers!
- 🎭 Playing with Values (Sounds Like a Hacker! 👁🗨)
- 🛑 let vs. const: The Big Difference!
- 🚨 Avoid var (For Your Own Good!)
- 🔢 Data Types: Different Kinds of Data!
- 🚀 Operators: Performing Magic with Data!
- 🎮 Play Around & Experiment!
- Bonus

Welcome, welcome:
Hello, I am back again with the new article of our "JavaScript" series as promised! In part 1 and part 2, we gained knowledge about what programming really is, why we are learning JavaScript as our first language, and how we can run JavaScript in our system in different ways.
Here in part 3 of this JavaScript series, we are going to taste some real essence of JavaScript. There are different things like variables, data types, different operations, and many more. Wait… wait… wait… don't get tensed after reading so many jargons! We will understand everything in a very simple manner, so do not take stress, just enjoy, focus, and relax…
🌟 Magic Trick: Dynamic Greeting! 🚀
Before diving in, let's add a little fun! Run this in your console:
let name = prompt("Enter your name:");
console.log(`Hello, ${name}! Welcome to JavaScript.`);
Try it out and see how JavaScript interacts with user input! Now, let’s get started with variables. 🎩✨
🏺 Variables: Your Storage Containers!
Think about your school days, maybe 7th or 8th grade. We were introduced to algebra, where we first saw an unknown number called "x"—then came "y"—and soon, the whole English alphabet was used as variables!
A variable is like a placeholder where values can change anytime. Think about calculating the result of 2x + 3x + 5
, where x = 5
. If we change x
to 7, the whole result changes!
In programming, a variable is just a container (like a jar where we store different grocery items) that holds data efficiently.
In JavaScript, we have three ways to declare variables:
let
const
var
(which we will avoid using!)
Let's create our first variable:
let x;
// We've created a variable 'x' but haven't assigned any value yet.
x = 10;
// Now, we assign the value 10 to 'x'.
console.log(x); // Output: 10
📝 Quick Check: 1️⃣ Create a variable year
, assign the current year, and print it to the console. 2️⃣ Create a variable name
, assign your name, and print it to the console.
🎭 Playing with Values (Sounds Like a Hacker! 👁🗨)
Let’s see how we can change the value of a variable.
let game = "cricket";
console.log(game); // Output: cricket
game = "football";
console.log(game); // Output: football
📝 Quick Check: Create a variable age
, assign a value, print it, then change the value and print it again.
🛑 let
vs. const
: The Big Difference!
🚀 Key takeaway:
let
allows you to change the value later.const
locks the value, meaning you cannot reassign it.
const number1 = 20;
console.log(number1); // Output: 20
number1 = 40; // ❌ ERROR: Assignment to constant variable!
So, always remember: Use const
when you don't want the value to change. In the upcoming articles of this series , we will see the specific use cases of const
in detail.
🚨 Avoid var
(For Your Own Good!)
JavaScript has evolved, and using var
can introduce unnecessary bugs and complexity. So, as per modern JavaScript standards, we will avoid using var
.
🚀 Homework: 1️⃣ Try creating different variables using let
and const
. 2️⃣ What happens when you try to reassign a const
variable? 3️⃣ Can you think of a real-world example where using const
makes more sense than let
?
🔢 Data Types: Different Kinds of Data!
Now that we know how to create variables, let’s talk about what kind of data we can store in them. JavaScript has different types of data, just like we have different kinds of items in a grocery store!
Here are the main types of data in JavaScript:
1️⃣ Numbers: Whole numbers or decimals (e.g., 5
, 10.5
).
2️⃣ Strings: Text enclosed in quotes (e.g., "Hello"
, 'JavaScript'
).
3️⃣ Booleans: true
or false
(like an ON/OFF switch).
4️⃣ Undefined: When a variable is declared but has no value.
5️⃣ Null: Represents an intentional empty value.
6️⃣ Objects: Collections of data (we’ll learn this later!).
7️⃣ Arrays: Lists of values (more on this later too!).
Let's see some examples:
let number = 25; // Number
typeof number; // Output: "number"
let text = "JavaScript is fun!"; // String : String value alwaus enclosed with quotes
typeof text; // Output: "string"
let isCodingFun = true; // Boolean
typeof isCodingFun; // Output: "boolean"
let unknown; // Undefined
typeof unknown; // Output: "undefined"
let empty = null; // Null
typeof empty; // Output: "object" (weird, but that's JavaScript!)
📝 Quick Check: 1️⃣ Create a variable for each data type and print the value in the console. 2️⃣ Use typeof
to check the type of each variable.
📦 Data Types at a Glance!
Data Type | Example | Description |
Number | let num = 25; | Represents numerical values, including decimals. |
String | let name = "Alice"; | Text enclosed in quotes. |
Boolean | let isHappy = true; | Can be true or false . |
Undefined | let x; | A variable declared but not assigned a value. |
Null | let empty = null; | Represents an intentional absence of a value. |
Object | let person = {name: "Bob", age: 30}; | Stores multiple values in a structured format. |
Array | let colors = ["red", "blue", "green"]; | A list of values. |
🚀 Operators: Performing Magic with Data!
In programming, operators are like the tools we use to manipulate data and perform different actions. Just like in real life, where we use tools like a calculator to add, subtract, multiply, or compare numbers, JavaScript operators help us process and control data efficiently.
For example, if you want to build a game where a player's score increases when they collect a coin, you'll need an arithmetic operator (+
). If you want to check if the player has enough points to level up, you'll need a comparison operator (>=
). And if you want to apply conditions (like checking if the player has a key or a password to unlock a door), logical operators (||
, &&
) help!
Now, let's explore these operators in action.
1️⃣ Arithmetic Operators
Used for mathematical operations:
let a = 10;
let b = 5;
console.log(a + b); // Addition -> 15
console.log(a - b); // Subtraction -> 5
console.log(a * b); // Multiplication -> 50
console.log(a / b); // Division -> 2
console.log(a % b); // Modulus (remainder) -> 0
2️⃣ Comparison Operators
Used to compare values:
console.log(10 > 5); // true
console.log(10 < 5); // false
console.log(10 == "10"); // true (loose comparison)
console.log(10 === "10"); // false (strict comparison)
3️⃣ Logical Operators
Used for logical conditions:
console.log(true && false); // false (AND)
console.log(true || false); // true (OR)
console.log(!true); // false (NOT)
📝 Quick Check: 1️⃣ Try different arithmetic operations. 2️⃣ Experiment with comparison and logical operators.
🎮 Play Around & Experiment!
This is just the beginning! Stay tuned for the next part, where we will explore functions, conditionals, and loops and also more depth discussion on data types. Until then, experiment, play around, and don’t hesitate to try your own magic with JavaScript! ✨🔥
🏆 Homework :
1️⃣ Basic Variable Practice
Create a variable
favoriteColor
and assign your favorite color as a string. Print it.Create a variable
birthYear
and assign the year you were born. Print it.
2️⃣ Changing Variable Values
Declare a variable
pet = "cat"
and print it.Change its value to
"dog"
and print it again.
3️⃣ let
vs const
Practice
Create a
const
variableearth = "Our Planet"
. Try changing its value. What happens?Create a
let
variableage
, assign your age, and then increase it by 1. Print the updated value.
4️⃣ Checking Data Types (typeof
)
Create variables for your name, age, height (decimal), and isStudent (true/false).
Use
console.log(typeof variableName)
to check each data type.
5️⃣ Simple Math with Operators
Create two numbers,
a = 10
andb = 5
.Print the result of
a + b
,a - b
,a * b
,a / b
, anda % b
.
6️⃣ Comparison Operators Practice
Check if
10 > 5
,10 < 5
, and10 == "10"
.What happens when you try
10 === "10"
?
7️⃣ Fun with prompt()
Ask the user for their name using
prompt()
and print a greeting:let userName = prompt("Enter your name:"); console.log("Hello, " + userName + "! Welcome to JavaScript.");
Run the code in the browser and observe how it works!
💬 Share your answers and thoughts in the comments. Let's learn together! 🚀
Bonus
If you are facing any trouble to understand any of these concept after trying with google search, feel free to contact me through email, X, or LinkedIn.
Subscribe to my newsletter
Read articles from Koustav Ganguly directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Koustav Ganguly
Koustav Ganguly
👋 Hi, I'm Koustav Ganguly—a Computer Science graduate with a passion for exploring the latest in technology and science. I thrive on simplifying complex topics, making learning accessible for all. Through my writing, I aim to break down intricate concepts into their simplest forms. Beyond coding, I'm a designer, a meticulous planner, and a problem solver, excelling not just at the code level but in overall design and management. Join me as I share insights, ideas, and solutions to inspire and empower learners and creators alike.