Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

Updated
โ€ข5 min read
JavaScript Operators: The Basics You Need to Know
A

I'm a Software engineer and recently graduated (2025) in computer engineering, I'm more into Fullstack Developement using Angular, Nodejs, Express js, Mongodb, currently getting my hands on React js and Next js. Connect me on Linkedin here: https://linkedin.com/in/amanpatel2529

Hey everyone, Aman here! ๐Ÿ‘‹

Whenever we write code, we are essentially giving the computer a set of instructions. We create variables to store our data (our nouns), but how do we actually do things with that data? How do we add numbers, compare passwords, or change values?

That is where Operators come in. Think of operators as the "verbs" of JavaScript. They are the symbols that tell the engine to perform mathematical calculations, compare different values, or make logical decisions.

Today, we are going to break down the everyday operators you will use constantly in your JavaScript journey. Open up your browser console (F12 or Right-Click -> Inspect -> Console) and let's get to work!


๐Ÿงฎ 1. Arithmetic Operators (The Math Stuff)

Let's start with the most familiar ones. Arithmetic operators take numerical values and return a single new number, exactly like a calculator.

  • Addition (+): Adds two numbers together.

  • Subtraction (-): Subtracts the right number from the left.

  • Multiplication (*): Multiplies two numbers.

  • Division (/): Divides the left number by the right.

console.log(10 + 5); // 15
console.log(20 - 8); // 12
console.log(4 * 3);  // 12
console.log(15 / 3); // 5

The Special One: Modulo (%) As a beginner, you might see % and think "percentage." In JavaScript, it actually means remainder. It divides the left number by the right number and gives you whatever is left over. It is incredibly useful for figuring out if a number is even or odd!

console.log(10 % 3); // 1 (Because 10 divided by 3 is 9, with 1 left over)
console.log(10 % 2); // 0 (10 divides evenly by 2, so it's an even number!)

๐Ÿ“ฅ 2. Assignment Operators (Storing Values)

We use these to assign a value to a variable.

  • Basic Assignment (=): You've seen this before. It puts the value on the right into the variable on the left.

  • Addition Assignment (+=): A shortcut to add a value to an existing variable.

  • Subtraction Assignment (-=): A shortcut to subtract a value from an existing variable.

let myScore = 50; // Basic assignment

// I scored 10 more points!
myScore += 10; // This is the exact same as writing: myScore = myScore + 10;
console.log(myScore); // 60

// I lost 5 points...
myScore -= 5; 
console.log(myScore); // 55

โš–๏ธ 3. Comparison Operators (Asking Questions)

Comparison operators compare two values and always return a Boolean: true or false. They are the core of decision-making (like in our if-else statements!).

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

  • Not equal (!=)

console.log(10 > 5);  // true
console.log(8 < 2);   // false
console.log(10 != 5); // true (10 is NOT equal to 5)

The Big Debate: == vs === This is a classic JavaScript interview question. Both of these check for equality, but they do it differently.

  • Loose Equality (==): This checks if the values are the same, but it ignores the data type. JavaScript will actually try to convert them to match behind the scenes.

  • Strict Equality (===): This checks if the values AND the data types are exactly the same.

console.log(5 == "5");  // true (JS converts the string "5" into a number, then compares)
console.log(5 === "5"); // false (One is a Number, one is a String. They are not strictly identical!)

Golden Rule: Always, always, always use === in modern JavaScript. It prevents weird, unexpected bugs in your code!


๐Ÿง  4. Logical Operators (Combining Questions)

Sometimes, one condition isn't enough. What if a user needs to have the correct email AND the correct password to log in? We use logical operators to combine multiple conditions.

  • AND (&&): Returns true ONLY if both sides are true.

  • OR (||): Returns true if at least one side is true.

  • NOT (!): Flips the boolean. True becomes false, and false becomes true.

let hasUsername = true;
let hasPassword = false;

// Using AND (&&)
console.log(hasUsername && hasPassword); // false (Because the password is missing!)

// Using OR (||)
let isWeekend = true;
let isHoliday = false;
console.log(isWeekend || isHoliday); // true (We only need one of these to be true to relax!)

// Using NOT (!)
let isRaining = true;
console.log(!isRaining); // false (It flips the value)

๐Ÿ‘จโ€๐Ÿ’ป Your Turn: The Assignment!

Ready to combine everything we just learned? Open up your console and try this out:

  1. Arithmetic: Create two variables holding numbers. Create a third variable that stores the result of multiplying them together, and print it.

  2. Comparison: Write a console.log() statement that compares the number 100 and the string "100" using loose equality (==), and another using strict equality (===). Note the difference in the output!

  3. Logical: Create an if statement that checks two variables: let age = 20; and let hasTicket = true;. Use the && operator to print "Enjoy the movie!" only if the age is greater than 18 AND they have a ticket.

Mastering operators means you can finally start building complex logic and math into your applications.