Skip to main content

Command Palette

Search for a command to run...

Understanding Operators in JavaScript

Updated
•5 min read•View as Markdown

When we write JavaScript programs, we often need to do calculations, compare values, or update variables.
To do these things we use operators.

Operators are symbols that tell JavaScript what action to perform on values.

For example:

5 + 3

Here + is an operator and it tells JavaScript to add two numbers.

In this article we will look at some common operators used in everyday JavaScript.


What Operators Are

An operator is a symbol that performs an operation on values.

Example:

10 + 5
  • 10 and 5 → operands

  • + → operator

Operators help us perform tasks like:

  • doing math

  • comparing values

  • checking conditions

  • updating variables

JavaScript has many operators, but here we will focus on the most common ones.


Arithmetic Operators

Arithmetic operators are used for basic math operations.

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

Example:

let a = 10
let b = 3

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

Output:

13
7
30
3.333...
1

The % operator returns the remainder after division.

Example:

10 % 3

Output:

1

Comparison Operators

Comparison operators are used to compare two values.

They usually return true or false.

Operator Meaning
== Equal value
=== Equal value and type
!= Not equal
> Greater than
< Less than

Example:

console.log(5 > 3)
console.log(5 < 3)

Output:

true
false

Difference Between and =

This is one thing that confuses many beginners.

Double equals ==

Checks only the value.

Example:

console.log(5 == "5")

Output:

true

JavaScript converts the string "5" into a number.


Triple equals ===

Checks both value and type.

Example:

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

Output:

false

Because:

  • 5 → number

  • "5" → string

So they are not the same type.

In modern JavaScript, developers usually prefer using ===.


Logical Operators

Logical operators are used to combine conditions.

Operator Meaning
&& AND
! NOT

Example:

let age = 20
let hasID = true

console.log(age > 18 && hasID)

Output:

true

Because both conditions are true.


Logical AND &&

Both conditions must be true.

true && true → true
true && false → false

Logical OR ||

At least one condition must be true.

true || false → true
false || false → false

Logical NOT !

Reverses the value.

!true

Output:

false

Truth Table for Logical Operators

AND Operator

A B A && B
true true true
true false false
false true false
false false false

OR Operator

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


Assignment Operators

Assignment operators are used to assign or update values.

The most common one is:

=

Example:

let x = 10

This assigns 10 to x.

There are also shortcut assignment operators.

Operator Example Meaning
+= x += 5 x = x + 5
-= x -= 5 x = x - 5

Example:

let score = 10

score += 5
console.log(score)

Output:

15

Simple Operator Categories

Operators
│
├── Arithmetic → + - * / %
├── Comparison → == === != > <
├── Logical → && || !
└── Assignment → = += -=

These are the operators you will use most often in everyday JavaScript code.


Practice Assignment

Try this small exercise.

1. Perform 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

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

Observe the difference.


3. Write a logical condition

Example:

let age = 20
let hasTicket = true

if (age >= 18 && hasTicket) {
  console.log("You can enter")
}

Final Thoughts

Operators are a basic but very important part of JavaScript.

We use them almost everywhere:

  • calculations

  • conditions

  • updating values

  • comparing data

Once you get comfortable with operators, writing JavaScript logic becomes much easier.

Practice using them in small examples and you will quickly get the hang of it.