Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

Updated
7 min read

When I started learning JavaScript, operators were one of those things that looked very easy at first. We use symbols like +, -, >, or = all the time, but in programming, each of them has a specific job.

In this article, we’ll understand what operators are, the main types of operators in JavaScript, and how we use them in everyday code.

What Are Operators in JavaScript?

Operators are special symbols that tell JavaScript to perform an action.

For example, if we write:

let sum = 10 + 5;

The + operator tells JavaScript to add the two values.

So in simple words, operators help us:

  • do calculations

  • compare values

  • combine conditions

  • assign values to variables

Arithmetic Operators

Arithmetic operators are used for basic math operations.

Here are the most common ones:

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

Let’s see them with code:

let a = 10; let b = 5; console.log(a + b); // 15 console.log(a - b); // 5 console.log(a * b); // 50 console.log(a / b); // 2 console.log(a % b); // 0

Why Is % Useful?

The modulus operator % gives the remainder.

console.log(10 % 3); // 1

This is useful when we want to check things like whether a number is even or odd.

console.log(4 % 2); // 0 console.log(5 % 2); // 1

If the remainder is 0, the number is even.

Comparison Operators

Comparison operators are used to compare two values.

They usually return either:

  • true

  • false

Here are some common comparison operators:

Operator Meaning Example
== Equal to 5 == "5"
=== Strict equal to 5 === "5"
!= Not equal to 5 != 3
> Greater than 10 > 5
< Less than 5 < 10

Let’s see some examples:

console.log(10 > 5); // true console.log(10 < 5); // false console.log(10 != 5); // true

Difference Between == and ===

This is one of the most important things for beginners to understand.

== checks value only

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

Here, JavaScript only checks the value, and it treats both as the same.

=== checks value and type

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

Here, JavaScript checks both:

  • the value

  • the data type

5 is a number, and "5" is a string, so the result is false.

Simple Rule

I try to remember it like this:

  • == means loose comparison

  • === means strict comparison

In most cases, it is better to use === because it is clearer and safer.

Logical Operators

Logical operators are used when we want to combine conditions.

The main logical operators are:

Operator Meaning Example
&& And age > 18 && isStudent
` `
! Not !isStudent

&& (AND)

This returns true only when both conditions are true.

let age = 20; let isStudent = true; console.log(age > 18 && isStudent); // true

If one condition is false, the result becomes false.

|| (OR)

This returns true if at least one condition is true.

let age = 16; let isStudent = true; console.log(age > 18 || isStudent); // true

Even though age > 18 is false, isStudent is true, so the result is true.

! (NOT)

This operator reverses the boolean value.

let isStudent = true; console.log(!isStudent); // false

If the value is true, ! makes it false.

If the value is false, ! makes it true.

Truth Table for Logical Operators

Here is a simple truth table:

| A | B | A && B | A || B | | --- | --- | --- | --- | | true | true | true | true | | true | false | false | true | | false | true | false | true | | false | false | false | false |

And for !:

A !A
true false
false true

Assignment Operators

Assignment operators are used to assign values to variables.

The most common one is =.

let age = 21;

This means we are assigning the value 21 to age.

There are also shortcut assignment operators.

Operator Meaning Example
= Assign x = 10
+= Add and assign x += 5
-= Subtract and assign x -= 5

Example:

let x = 10; x += 5; console.log(x); // 15 x -= 3; console.log(x); // 12

This is a shorter way of writing:

x = x + 5; x = x - 3;

Operator Categories at a Glance

Here is a simple table to revise everything quickly:

Category Operators
Arithmetic +, -, *, /, %
Comparison ==, ===, !=, >, <
Logical &&, `
Assignment =, +=, -=

Practical Examples

Let’s try a few simple examples.

1. Arithmetic Operations on Two Numbers

let a = 12; let b = 4; console.log(a + b); // 16 console.log(a - b); // 8 console.log(a * b); // 48 console.log(a / b); // 3 console.log(a % b); // 0

2. Compare Two Values Using == and ===

console.log(10 == "10"); // true console.log(10 === "10"); // false

This clearly shows why === is stricter.

3. Small Condition Using Logical Operators

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

This means the person is an adult and also has an ID.

Easy Way to Remember

Here’s the simple way I think about operators:

  • arithmetic operators help us do math

  • comparison operators help us compare values

  • logical operators help us combine conditions

  • assignment operators help us store or update values

Practice Task

Try this on your own:

let a = 15; let b = 4; console.log(a + b); console.log(a - b); console.log(a * b); console.log(a / b); console.log(a % b);

Now try these:

  • compare 5 and "5" using both == and ===

  • create two boolean values and test them with && and ||

  • use += and -= on a number variable

These small exercises will make the operators much easier to understand.

Final Thoughts

Operators are a very basic part of JavaScript, but they are used everywhere. Once I started practicing them with small examples, they became much easier to understand.

If you are a beginner, don’t try to memorize everything at once. Just focus on what each operator does and practice with small code snippets.

Once these basics are clear, writing conditions, calculations, and logic in JavaScript becomes much more natural.