JavaScript Basis - The Language of the web
JS Variables and Data Types, Operators and Control flow explained
Here, I'll explain the topics above using real-life examples. I've made them entertaining so you won't get bored! ๐
JavaScript Variables and Data Types
What are variables?
Think of a variable like a box ๐ฆ where you store things, or you have 100% seen this container to store your cookies๐ซ, my fav; chocolate one, specially! Variables are the same, they can store various types of data such as strings, numbers, booleans, and more. If your brain had variables, you'd have:
let favoriteSnack = "Chips";
let age = 21;
const birthYear = 2003; // This will never change
Here,
let
allow changes (you can change your favorite snack, of course, by the time every fav thing is boring).const
stays the same forever (your birth year won't change, unless you're a time traveler ๐).
Example: Ordering Pizza ๐
Imagine you're ordering pizza (because why not?). You need to store your order details:
let customerName = "Tvisha Sharma"; // String
let pizzaSize = "Medium"; // String
let numberOfPizzas = 2; // Number
let isvegetarian = true; // Boolean
let toppings = ["Mushrooms", "Olives", "Cheese"]; // Array
let orderDetails = { // Object(though object itself is the vast thing to explore, we'll do in further meets!)
orderId: 101,
customer: "Tvisha Sharma",
size: "Medium",
quantity: 2,
specialInstructions: null,
};
console.log(orderDetails.customer); //Tvisha Sharma
Now, you can access orderDetails.customer
to know whoโs paying for the pizza, too bad js canโt pay for it. ๐
JavaScript Data Types
JavaScript has two types of data:
โ Primitive (Number, String, Boolean, Undefined, Null)
โ Non-Primitive (Objects, Arrays, Functions)
So basically, JavaScript lets you store everything from your name to a complete shopping list. ๐
Java Script Operators
Operators in JavaScript are like the tools you use in real life:
Arithmetic Operators (
+
,-
,*
,/
,%
) โ Like your calculator ๐งฎComparison Operators (
>
,<
,==
,===
) โ Used when arguing with friends:
โIs my pizza bigger than yours?โ ๐๐ปLogical Operators (
&&
,||
,!
) โ Used when making excuses:
"If I'm not full yet AND you still have pizza left, then..." ๐
Example: Gym Membership Discount ๐๏ธโโ๏ธ
Gyms usually give discounts if you sign up for 6 months or more (but sadly, not for just thinking about exercising). ๐
let membershipMonths = 7;
let discount = membershipMonths >= 6 ? "Congrats! Discount unlocked! ๐" : "No discount, sorry. ๐ญ";
console.log(discount);
๐ Explanation:
>=
checks if the membership is 6 or more months.The
? :
(ternary operator) is like JavaScript flipping a coin:If true, it gives
"Congrats! Discount unlocked! ๐"
If false, it returns
"No discount, sorry. ๐ญ"
If only gyms gave discounts based on how often we actually go. ๐
Control Flow (if-else & switch)
JavaScript makes decisions just like we doโexcept it doesn't overthink things at 2 AM!๐
Example: Traffic Light Logic ๐ฆ
Imagine youโre driving (or just jaywalking like a rebel โ ๏ธ). JavaScript will handle the traffic lights:
Using if-else :
let light = "red";
if (light === "green") {
console.log("Go! ๐๏ธ");
} else if (light === "yellow") {
console.log("Slow down! ๐");
} else if (light === "red") {
console.log("STOP! ๐ซ");
} else {
console.log("Bro, are we on Mars? ๐ฝ");
}
๐ Explanation:
Really need so!? We are not from mars though! ๐คท๐ปโโ๏ธ
Using if-else :
switch (light) {
case "green":
console.log("Go! ๐๏ธ");
break;
case "yellow":
console.log("Slow down! ๐");
break;
case "red":
console.log("STOP! ๐ซ");
break;
default:
console.log("Is this light broken? ๐ค");
}
๐ Explanation:
switch
is like an if-else with style.Each
case
checks the value oflight
and executes the correct block.The
break;
stops execution after finding the right match.
If only real-life drivers followed JavaScript's logic. ๐๐จ
Final Thoughts
So, what did we learn today?
โ
Variables = Your memory storage (like a backpack ๐)
โ
Operators = Math, comparisons, and bad excuses ๐คท
โ
Control Flow = JavaScript making decisions (like our mommy) ๐ฉโ๐ฆ
Now you know JavaScript basics, and youโre officially smarter than your WiFi router. ๐๐
Have fun guys! Thanks for your time.
I'll be posting more blogs like this, so stay tuned! ๐ซถ๐ป