Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

Updated
7 min read
JavaScript Operators: The Basics You Need to Know
S
A front-end developer who’s always learning, building projects, and writing blogs to simplify web concepts

If you’re starting your JavaScript journey, operators are one of the first and most important concepts you’ll encounter. Operators allow you to perform calculations, compare values, and make decisions in your code.

In this article, we’ll cover the most commonly used JavaScript operators with simple examples you’ll use every day as a developer.

What Are Operators in JavaScript?

Before knowing what operators are in JavaScript, we first need to understand what operators are in real life or in normal mathematics. Operators are symbols that help us perform operations like addition, subtraction, multiplication, and division between two values.

For example, in mathematics, operators allow us to find the sum, multiply numbers, or perform other calculations. In the same way, JavaScript operators help us perform operations between values or variables inside our code.

For example

let sum = 10 + 5;

console.log(sum);

In this example, we have two operands, 10 and 5. Between them, we use the **plus (+) operator.

The + The operator tells JavaScript to add both numbers together. As a result, JavaScript calculates the sum of 10 and 5, which is 15, and stores it in the variable sum.

Arithmetic operators (+, -, *, /, %)

Arithmetic operators are the most commonly used operators in almost every programming language, not just JavaScript. These operators are mainly used to perform mathematical calculations such as addition, subtraction, multiplication, and division.

Let’s understand each arithmetic operator one by one.

Addition Operator (+)

The addition operator is used to add two numbers.

let sum = 10 + 5;
console.log(sum); // 15

In this example**,** 10 and 5 are operands, and the + operator adds them together. JavaScript calculates the sum and stores the result (15) in the variable sum.

Subtraction Operator (-)

The subtraction operator is used to subtract one number from another.

let result = 10 - 5;
console.log(result); // 5

Multiplication Operator (*)

The multiplication operator is used to multiply two numbers.

let product = 10 * 5;
console.log(product); // 50

Division Operator (/)

The division operator is used to divide one number by another.

let value = 10 / 5;
console.log(value); // 2

Modulus Operator (%)

The modulus operator returns the remainder after division.

let remainder = 10 % 3;
console.log(remainder); // 1

Comparison operators (==, ===, !=, >, <)

As the name suggests, comparison operators are used to compare two values with each other. These operators check a condition and always return a Boolean value, either true or false. Comparison operators help us decide whether one value is equal to, greater than, or less than another value.

Equal To (==)

The == operator checks only the value, not the data type.

console.log(5 == "5"); // true

Strict Equal To (===)

The === operator checks both value and data type.

console.log(5 === "5"); // false

Difference between == and === clearly

As you can see in the examples, the == operator checks only the value of the compared operands, not the data type. So, if the values are the same but their types are different, == still returns true. This behavior can make the code buggy and unpredictable. Because of this reason, in JavaScript we prefer using the === operator, which strictly checks both the value and the data type.

Not Equal To (!=)

The != operator checks whether two values are not equal.

console.log(5 != 3); // true

5 is not equal to 3, JavaScript returns true.

Greater Than (>)

console.log(10 > 5); // true

JavaScript checks if 10 is greater than 5, which is true.

Less Than (<)

console.log(3 < 8); // true

Logical operators (&&, ||, !)

There are only three types of logical operators in JavaScript. Let’s explore them one by one.

Logical operators are used to compare or combine conditions. They help us make decisions in our code by checking whether one or more conditions are true or false.

In some cases, both conditions must be true to get a true result, while in other cases, only one condition needs to be true.

AND Operator (&&)

The AND operator returns true only if both conditions are true.

let age = 20;
let hasID = true;

console.log(age >= 18 && hasID); // true

age >= 18 → true

hasID → true

Since both conditions are true, the result is true.

OR Operator (||)

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

let isWeekend = false;
let isHoliday = true;

console.log(isWeekend || isHoliday); // true

Even though isWeekend is false, isHoliday is true, so the overall result is true.

NOT Operator (!)

The NOT operator reverses the result of a condition.

let isLoggedIn = false;

console.log(!isLoggedIn); // true

The NOT operator changes false to true and true to false.

Truth Table for Logical Operators

A truth table helps us understand how logical operators behave with different combinations of true and false.

AND Operator (&&) – Truth Table

The AND operator returns true only when both conditions are true.

OR Operator (||) – Truth Table

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

NOT Operator (!) – Truth Table

The NOT operator simply reverses the value.

Assignment operators (=, +=, -=)

Assignment operators are used to assign values to variables and update existing values. Instead of writing long expressions again and again, assignment operators help us write shorter and cleaner code.

Let’s understand each step by step.

Assignment Operator (=)

The = operator is used to assign a value to a variable.

let number = 10;

Here, the value 10 is assigned to the variable number.
Now, number stores 10 in memory.

Add and Assign Operator (+=)

The += operator adds a value to a variable and stores the updated result back in the same variable.

let score = 10;
score += 5;

console.log(score); // 15

score += 5 means:
score = score + 5. JavaScript adds 5 to the current value of score.The new value (15) is saved back into the score

Subtract and Assign Operator (-=)

The -= The operator subtracts a value from a variable and updates the variable.

let balance = 20;
balance -= 7;

console.log(balance); // 13

balance -= 7 means:
balance = balance - 7. JavaScript subtracts 7 from 20. The updated value (13) is stored in the balance

Conclusion

JavaScript operators are one of the most important building blocks of the language. They allow us to perform calculations, compare values, and make decisions in our code.

In this article, we learned:

  • Arithmetic operators to perform basic math operations

  • Comparison operators to compare values and get true or false results

  • Logical operators to combine multiple conditions

  • Assignment operators to store and update values efficiently

Understanding these operators makes it easier to write clean, readable, and bug-free code. Almost every JavaScript feature, such as if statements, loops, and functions, depends on operators.

JavaScript Journey: From Basics to Core Concepts

Part 2 of 29

This series documents my journey of learning JavaScript and breaking down important concepts in a simple way. Each article covers a core JavaScript topic with clear explanations and beginner-friendly examples. From basic concepts to essential JavaScript features, the goal of this series is to make JavaScript easier to understand while practicing and sharing what I learn.

Up next

The Magic of this, call(), apply(), and bind() in JavaScript

What does this mean in JavaScript? In the title, I used the phrase “The Magic of JavaScript.”Of course, there is no real magic in programming; everything follows clear rules and logic.But JavaScript h