JavaScriptCohort2026ChaiCode

JavaScript Operators: The Basics You Need to Know

Hello everyone! If you are starting your JavaScript journey, you will quickly realize that programming isn't just about storing data in variables or…

Kenil Patel
Kenil PatelFull Stack Engineer
JavaScript Operators: The Basics You Need to Know

Hello everyone! If you are starting your JavaScript journey, you will quickly realize that programming isn't just about storing data in variables or arrays. We also need to do things with that data—like adding numbers, comparing values, or making decisions.

That is exactly where Operators come in! Today, we are going to break down the most common JavaScript operators you will use every single day. Let's dive in!

1. What Are Operators?

In simple terms, an operator is a special symbol that performs a specific task or calculation on one or more values.

Think of basic math from school. If you write 5 + 3, the + symbol is the operator telling the computer to add the two numbers together. The numbers 5 and 3 are called operands. JavaScript has several types of operators, and we are going to look at the four most important ones.


2. Arithmetic Operators (The Math Stuff)

These are the operators you are probably already familiar with. We use them to perform basic mathematical calculations.

  • + (Addition): Adds values together.

  • - (Subtraction): Subtracts one value from another.

  • * (Multiplication): Multiplies values.

  • / (Division): Divides one value by another.

  • % (Modulo): This one sounds fancy, but it simply gives you the remainder of a division. For example, if you divide 10 by 3, the remainder is 1.

Let's see them in action:

JavaScript

let a = 10;
let b = 3;

console.log(a + b); // Output: 13
console.log(a - b); // Output: 7
console.log(a * b); // Output: 30
console.log(a / b); // Output: 3.333...
console.log(a % b); // Output: 1 (Because 10 divided by 3 leaves a remainder of 1)

3. Assignment Operators (Saving Data)

Assignment operators are used to assign a value to a variable.

  • = (Simple Assignment): Assigns the right-side value to the left-side variable.

  • += (Add and Assign): Adds a value to the variable and saves the new result.

  • -= (Subtract and Assign): Subtracts a value from the variable and saves the new result.

Console Example:

JavaScript

let score = 50; // We used '=' to assign 50 to the score

score += 10; // This is a shortcut for: score = score + 10
console.log(score); // Output: 60

score -= 5; // This is a shortcut for: score = score - 5
console.log(score); // Output: 55

4. Comparison Operators (Who is the Boss?) ️

We use comparison operators to compare two values. They always return a boolean result: either true or false.

  • > (Greater than)

  • < (Less than)

  • != (Not equal to)

  • == (Loose Equality)

  • === (Strict Equality)

The Big Trap: == vs ===

This is the most common mistake beginners make!

  • == (Loose Equality): This only checks if the values are the same. It does not care about the data type. It will happily convert a string into a number to make a match.

  • === (Strict Equality): This checks BOTH the value and the data type. If they don't match exactly, it returns false. Always use === to avoid weird bugs!

Console Example:

JavaScript

let age = 18; // Number
let stringAge = "18"; // String

console.log(age > 10); // Output: true
console.log(age < 5);  // Output: false

// Loose vs Strict
console.log(age == stringAge);  // Output: true (JavaScript converted the string to a number for you)
console.log(age === stringAge); // Output: false (Different data types! One is a number, one is a string)

5. Logical Operators (Making Decisions)

Logical operators are used when you have multiple conditions to check at the same time.

  • && (AND): Returns true ONLY if all conditions are true. (Like needing a ticket AND an ID to enter a movie).

  • || (OR): Returns true if at least one condition is true. (Like paying with a Credit Card OR Cash).

  • ! (NOT): Flips the result. If something is true, it makes it false.

Console Example:

JavaScript

let hasTicket = true;
let hasID = false;

// AND (&&) requires both to be true
console.log(hasTicket && hasID); // Output: false

// OR (||) requires only one to be true
console.log(hasTicket || hasID); // Output: true

// NOT (!) flips the boolean
console.log(!hasTicket); // Output: false

Practice Time: Assignment For You!

Reading code is great, but writing it is how you actually learn! Open up your code editor and try these three tasks:

  1. Arithmetic: Create two variables with numbers. Add them, multiply them, and print the results to the console.

  2. Comparison: Create a variable with the number 25 and another with the string "25". Compare them using both == and === and console.log the results.

  3. Logical: Create a small scenario using && or ||. For example, create variables for isRaining and hasUmbrella, and write a condition to check if it's safe to go outside!

Happy Coding!