Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

Updated
4 min readView as Markdown

When writing JavaScript programs, we often need to perform operations on values.

For example:

  • Adding two numbers

  • Comparing two values

  • Checking multiple conditions

These operations are done using operators.

In this article, we will learn the most common JavaScript operators used in everyday programming.

What Are Operators?

Operators are symbols used to perform operations on values or variables.

Example:

let sum = 5 + 3;

Here:

  • 5 and 3 are operands

  • + is the operator

The operator performs an action on the values.

Output:

8

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

Operator Meaning Example
+ Addition 5 + 2
- Subtraction 5 - 2
* Multiplication 5 * 2
/ Division 10 / 2
% Modulus (remainder) 5 % 2

Example

let a = 10;
let b = 5;

console.log(a + b); // addition
console.log(a - b); // subtraction
console.log(a * b); // multiplication
console.log(a / b); // division
console.log(a % b); // remainder

Output

15
5
50
2
0

The % operator returns the remainder after division.

Example:

7 % 2 = 1

Comparison Operators

Comparison operators are used to compare two values.

They return either true or false.

Operator Meaning
== Equal to
=== Strict equal
!= Not equal
> Greater than
< Less than

Example

let x = 10;
let y = 5;

console.log(x > y);
console.log(x < y);
console.log(x == y);

Output

true
false
false

Difference Between === and ==

This is very important in JavaScript.

== (Loose Equality)

== compares values only.

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

Output

true

JavaScript converts "5" to number.

=== (Strict Equality)

=== compares value and type.

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

Output

false

Because:

  • 5 → number

  • "5" → string

Most developers prefer === because it avoids unexpected conversions.

Logical Operators

Logical operators are used when working with multiple conditions.

Operator Meaning
&& AND
! NOT

AND Operator (&&)

Both conditions must be true.

let age = 20;

if (age > 18 && age < 60) {
  console.log("You are eligible to work");
}

OR Operator (||)

At least one condition must be true.

let day = "Sunday";

if (day === "Saturday" || day === "Sunday") {
  console.log("It is weekend");
}

NOT Operator (!)

Reverses the result.

let isLoggedIn = false;

console.log(!isLoggedIn);

Output

true

Assignment Operators

Assignment operators are used to assign values to variables.

Operator Example Meaning
= x = 5 Assign value
+= x += 2 x = x + 2
-= x -= 2 x = x - 2

Example

let x = 10;

x += 5;
console.log(x);

x -= 3;
console.log(x);

Output

15
12

These operators make code shorter and cleaner.

Assignment Examples

1. Arithmetic Operations

let a = 8;
let b = 4;

console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);

2. Compare Values Using === and ==

let num = 5;

console.log(num == "5");
console.log(num === "5");

Output

true
false

3. Logical Condition Example

let age = 25;
let hasLicense = true;

if (age >= 18 && hasLicense) {
  console.log("You can drive");
}

Real-Life Example

Think about logging into a website.

To log in successfully:

  • Username must be correct

  • Password must be correct

In code:

if (username === "admin" && password === "1234") {
  console.log("Login successful");
}

Both conditions must be true.

Conclusion

Operators are an important part of JavaScript because they allow us to perform operations and make decisions in programs.

The most commonly used operators include:

  • Arithmetic operators for calculations

  • Comparison operators for comparing values

  • Logical operators for combining conditions

  • Assignment operators for updating variables

Understanding these basics will help you write cleaner and more effective JavaScript code.