Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

JavaScript operators with simple examples ⚡➕ Compare values 🔍 build logic 🧠

Published
•5 min read
JavaScript Operators: The Basics You Need to Know

When you start learning JavaScript, operators are one of those things you begin using almost immediately. Sometimes you don’t even notice it at first. Every time you add numbers, compare values, or check a condition inside an if statement, you’re using operators.

Simply put, operators are symbols that perform operations on values. The values being used are called operands. For example, if you write 5 + 3, the plus symbol is the operator and the numbers are the operands.

These operators appear everywhere in JavaScript programs. Whether you are doing simple calculations or controlling the flow of your code, they quietly power a lot of the logic behind the scenes.

Let’s go through the most commonly used ones.

Arithmetic Operators

Arithmetic operators are used for mathematical calculations. If you’ve done basic math in school, these will feel very familiar.

JavaScript supports addition, subtraction, multiplication, division, and modulus. These operators help you perform everyday calculations inside your program.

Here’s a simple example.

let apples = 12
let friends = 4

let total = apples + friends
let difference = apples - friends
let product = apples * friends
let division = apples / friends
let remainder = apples % friends

console.log("Total:", total)
console.log("Difference:", difference)
console.log("Product:", product)
console.log("Division:", division)
console.log("Remainder:", remainder)

When you run this code, JavaScript performs each calculation and prints the result in the console.

The modulus operator is a bit different from the others. Instead of giving the division result, it returns the remainder.

This operator is often used when checking whether a number is even or odd.

let number = 15

console.log(number % 2)

If the output is 0, the number is even. If the output is 1, the number is odd.

Arithmetic operators are commonly used in counters, loops, calculations, and many other everyday programming situations.

Comparison Operators

Comparison operators help us compare two values. Instead of returning a number, they return a boolean value, which means the result will be either true or false.

These are very useful when writing conditions in programs.

let score = 75
let passingMarks = 50

console.log(score > passingMarks)
console.log(score < passingMarks)

The first comparison returns true because 75 is greater than 50. The second comparison returns false.

Now let’s look at something that confuses many beginners: the difference between double equals and triple equals.

The double equals operator checks if two values are equal after JavaScript converts their types if needed.

console.log(10 == "10")

This prints true because JavaScript converts the string "10" into the number 10 before comparing.

The triple equals operator is stricter. It checks both the value and the data type.

console.log(10 === "10")

This returns false because one value is a number and the other is a string.

In modern JavaScript, developers usually prefer the triple equals operator because it avoids unexpected type conversions.

Logical Operators

Logical operators allow us to combine multiple conditions together. They are commonly used in decision making inside if statements.

JavaScript mainly uses three logical operators: AND, OR, and NOT.

The AND operator checks whether both conditions are true.

let age = 20
let hasID = true

console.log(age >= 18 && hasID)

This will only return true if the person is at least 18 years old and has an ID.

The OR operator checks if at least one condition is true.

let hasCoupon = false
let isPremiumMember = true

console.log(hasCoupon || isPremiumMember)

Even though the person does not have a coupon, the result will still be true because they are a premium member.

The NOT operator simply flips a boolean value.

let loggedIn = false

console.log(!loggedIn)

Since loggedIn is false, the result becomes true.

Logical operators are extremely useful when writing conditions that depend on multiple factors.

Assignment Operators

Assignment operators are used to assign values to variables.

The most common one is the equals operator, which simply stores a value in a variable.

let balance = 500

JavaScript also provides shorthand assignment operators that combine arithmetic with assignment.

let points = 10

points += 5
console.log(points)

points -= 3
console.log(points)

The statement points plus equals 5 is just a shorter way of writing points equals points plus 5.

Similarly, points minus equals 3 is equivalent to writing points equals points minus 3.

These shorthand operators make the code shorter and easier to read.

A Small Practice Idea

A quick way to get comfortable with operators is to try a few small experiments in the console.

I am sharing you few tasks that you should be doing if you are not well versed with operators, these are very simple ones. I am not attaching their answers.
Kindly practice them if you are a beginner either in your IDE or on browser.

First, perform some arithmetic operations with two numbers.

let a = 8
let b = 3

console.log("Addition:", a + b)
console.log("Multiplication:", a * b)
console.log("Remainder:", a % b)

Next, compare two values using both equality operators.

let value1 = 5
let value2 = "5"

console.log(value1 == value2)
console.log(value1 === value2)

Finally, try writing a small condition using logical operators.

let temperature = 32
let isSunny = true

if (temperature > 30 && isSunny) {
    console.log("Good day for ice cream")
}

Operators may look like tiny symbols, but they play a huge role in programming. Almost every piece of JavaScript logic depends on them in some way. Once you get comfortable using them, writing conditions, calculations, and decision-making code becomes much more natural.

Hope it was helpful, sometimes writing blog for such direct concepts becomes a bit tricky if you don't cover everything from scratch considering that this is very basic and everyone must be aware of these, Hence I deliberately tried to cover everything from scratch.