JavaScript Basis - The Language of the web

JavaScript Basis - The Language of the web

JS Variables and Data Types, Operators and Control flow explained

ยท

4 min read

Here, I'll explain the topics above using real-life examples. I've made them entertaining so you won't get bored! ๐Ÿ˜‰

  1. 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. ๐Ÿ˜

  1. 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 of light 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! ๐Ÿซถ๐Ÿป

ย