Skip to main content

Command Palette

Search for a command to run...

Scoring and Stats: JavaScript Operators Explained 🔢

Updated
3 min readView as Markdown
Scoring and Stats: JavaScript Operators Explained 🔢

In football, everything is a numbers game. How many goals were scored? Who has more possession? Is the striker faster than the defender? In JavaScript, we answer these questions and perform these actions using Operators.

Operators are the symbols we use to perform operations on our data (variables). If your variables are the players, operators are the movements and interactions they make on the pitch.

1. Arithmetic Operators (The Scoreboard)

These are your basic math tools. We use them to calculate scores, distance covered, or even the remaining time in a match.

  • + (Addition): goals + 1

  • - (Subtraction): time - 10

  • * (Multiplication): playerValue * 2

  • / (Division): totalGoals / matchesPlayed

  • % (Modulus/Remainder): This tells you what's left over after division.

let goalsFirstHalf = 2;
let goalsSecondHalf = 1;

let finalScore = goalsFirstHalf + goalsSecondHalf; // 3
let goalsDiff = goalsFirstHalf - goalsSecondHalf; // 1

2. Assignment Operators (Setting the Value)

You’ve already used =, but did you know you can do math and assign a value at the same time? It’s a shortcut for busy managers.

  • = : Assigns a value.

  • += : Adds and then assigns.

  • -= : Subtracts and then assigns.

let cr7Goals = 800;
cr7Goals += 1; // Same as cr7Goals = cr7Goals + 1;
// New total: 801

3. Comparison Operators (The Scout’s Report)

Is your winger faster than the opposing defender? Comparison operators check two values and return a Boolean (true or false).

  • == (Loose Equality): Checks value only.

  • === (Strict Equality): Checks value and data type. (Always use this!)

  • != (Not Equal)

  • > (Greater Than) / < (Less Than)

The "Referee" Rule: == is like a lazy referee. He thinks "7" (string) and 7 (number) are the same. === is a strict referee who sees they are different types!


4. Logical Operators (The Match Strategy)

Sometimes a decision depends on more than one condition. This is where your logic kicks in.

  • && (AND): Both conditions must be true. (e.g., Has ball AND Near goal).

  • || (OR): At least one condition must be true. (e.g., Score a goal OR Keep a clean sheet).

  • ! (NOT): Flips the result (e.g., !isInjured).

truth table for logical operators AND OR NOT, AI generated

🏆 Daily Drill: The Stats Challenge ⚽

Head to your console and run these three drills to master your operators:

  1. Match Math: Create two variables, goals and assists. Perform all 5 arithmetic operations on them and print the results.

  2. The Strict Ref: Compare the number 10 and the string "10" using both == and ===. Observe the difference in the console.

  3. The Scouting Logic: Write a condition using && that checks if a player's pace is over 90 and their stamina is over 80.

Which operator do you think is the most used in a real match? Let me know in the comments!