Late-Night Lobby (Operators)

Welcome back, bro! In our last session, we created our storage chests (Variables) and filled them with loot (Data Types). But right now, those boxes are just sitting there doing absolutely nothing.
To make a game or any web app actually work, we need to calculate scores, compare levels, and make decisions. We need Operators.
Forget the boring math textbook definitions. Imagine it’s 2 AM, and me (Mayur), Alok, and Abhinav just booted up a multiplayer game. Here is how JavaScript operators are secretly running our entire gaming session.
1. Scoreboard (Arithmetic Operators)
Every time we play, the game has to constantly update our points. This is where basic math operators come in: + (Add), - (Subtract), * (Multiply), and / (Divide).
let mayurScore = 100;
let alokScore = 100;
// I got a headshot!
mayurScore = mayurScore + 50;
// Alok fell off the map (classic Alok) 💀
alokScore = alokScore - 20;
The Loot Split (And the % trick): We just defeated a boss and a chest dropped 100 gold coins. There are 3 of us. If we use division (100 / 3), JavaScript gives us 33.333333333333336. Nobody wants 0.33 of a digital coin.
Enter the Modulo (%) operator! Beginners always think % means percentage. It doesn’t! It means: "If I divide these numbers, what is the remainder?"
let totalGold = 100;
let players = 3;
let leftoverGold = totalGold % players;
console.log(leftoverGold); // Output: 1
Everyone gets 33 coins, and since 100 % 3 = 1, there is exactly 1 coin left over. (And obviously, since I'm hosting the lobby, I'm keeping it).
2. "I'm Too Lazy to Type" Update (Assignment Operators)
Writing mayurScore = mayurScore + 50 every single time I get a kill is exhausting. Developers are inherently lazy (we call it being "efficient").
Instead of writing the variable name twice, JavaScript gives us Assignment Operators like +=, -=, and *=.
// The old, boring way
mayurScore = mayurScore + 50;
// The Pro-Gamer way
mayurScore += 50;
alokScore -= 20; // Alok fell off the map again...
The Ultimate Shortcut (++ and --): What if we just want to count how many matches we've played by adding exactly 1 each time?
let matchesPlayed = 5;
// Adds exactly 1 to the current value
matchesPlayed++;
console.log(matchesPlayed); // Output: 6
3. Who is the Boss? (Comparison Operators)
You can't have a multiplayer game without a little toxicity and comparing stats. JavaScript lets us compare variables using <, >, <=, and >=. These always return a Boolean (true or false).
let myLevel = 50;
let abhinavLevel = 45;
console.log(myLevel > abhinavLevel); // true (I'm the boss here)
console.log(myLevel <= 50); // true (I am exactly 50 or less)
Bomb: == vs ===
This is the most important rule in JavaScript comparison.
Let's say to join the VIP server, your level must be exactly 50.
==(Loose Equality) is like a terrible, sleepy bouncer at a club. If the server needs the Number50, but you give it the String"50", the==bouncer says, "Eh, looks close enough, go on in."===(Strict Equality) is the strict, terrifying bouncer. It checks both the Value AND the Data Type. If you bring a String"50", it kicks you out.
let requiredLevel = 50; // Number
let playerInput = "50"; // String
console.log(requiredLevel == playerInput); // true (Terrible! Don't use this)
console.log(requiredLevel === playerInput); // false (Perfect! Security is tight)
Rule of thumb: Always, ALWAYS use ===. Pretend == doesn't exist.
4. Squad Logic (Logical Operators)
This is real-life gamer logic. Before we queue up for a match, we have rules about who we play with. JavaScript handles this using Logical Operators: && (AND), || (OR), and ! (NOT).
&& (AND) Operator: Both sides MUST be true. I will only play the super-sweaty Ranked mode if BOTH Alok AND Abhinav are online.
let isAlokOnline = true;
let isAbhinavOnline = true;
let playRanked = isAlokOnline && isAbhinavOnline;
console.log("Playing Ranked? ", playRanked); // true! Both are here.
|| (OR) Operator: Only ONE side needs to be true. I'll play casual matches if Alok OR Tasnish is online. I just need one of them.
let isAlokOnline = false;
let isTasnishOnline = true;
let playCasual = isAlokOnline || isTasnishOnline;
console.log("Playing Casual? ", playCasual); // true! At least Tasnish is here.
! (NOT) Operator (Uno Reverse Card): You know that one friend who always says, "Nah bro, I'm not playing tonight"? We don't accept that logic. The ! operator flips a boolean completely. If he says false, we make it true.
let abhinavWantsToPlay = false;
// If he says no, hit him with the ! to make it a yes. Force him into the lobby!
let forceIntoLobby = !abhinavWantsToPlay;
console.log("Is Abhinav playing? ", forceIntoLobby); // Output: true 😂
What's Next?
Now you know how to do the math, compare the stats, and set up the squad logic.
But right now, our code just runs straight from top to bottom. What if we want the game to say "If Mayur wins, show a victory screen, ELSE show a game over screen"?
In the next post, we are diving into Control Flow (If, Else, and Switch) so we can finally let our code make its own decisions. Grab a fresh chai, and I'll see you there!




