Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators

A Guide to Programming Operators in JavaScript Language

Updated
9 min read
JavaScript Operators
S

Frontend Developer 💻 | Fueled by curiosity and Tea ☕ | Always learning and exploring new technologies.

In the world of programming, if variables are the nouns and functions are the sentences, then operators are the verbs.

They are the symbols that trigger action, transforming raw data into meaningful results.

Operators

Operators are for Mathematical and Logical Computations.

Whether you are working in JavaScript, C++, Python, or Java, the symbols and works of most operators remains remarkably consistent.

There are different types of JavaScript operators:

  • Arithmetic Operators

  • Assignment Operators

  • Comparison Operators

  • Logical Operators

  • Ternary Operators

Arithmetic Operators

If you are here, then I am sure you have done calculations like finding the sum of two numbers or converting a CTC to a monthly salary. In those moments, you were already using arithmetic operators.

So, Arithmetic operators are used to perform mathematical operations on numbers. They are generally used to perform addition, subtraction, multiplication, division, modulo, and exponential operations.

Operator

Name

Example

Result

+

Addition

10 + 5

15

-

Subtraction

10 - 5

5

*

Multiplication

10 * 5

50

/

Division

10 / 4

2.5

%

Modulus

10 % 3

1 (remainder)

**

Exponentiation

2 ** 3 (cube of 2)

8

Assignment Operators

Assignment operators are used to assign values to variables.

Basic Assignment (Equal to =)

The most common operator. It takes the value on the right and puts it into the variable on the left.

  • Example: x = 3;

Compound Assignment

As a developer, you will often want to change a value that is already there like adding some value to a score in a game. Instead of writing a long statements, we use shortcuts.

Commonly used Compound Assignment:

Operator

Name

Example

Equivalent to

+=

Addition Assignment

x += 5

x = x + 5

-=

Subtraction Assignment

x -= 2

x = x - 2

*=

Multiplication Assignment

x *= 3

x = x * 3

/=

Division Assignment

x /= 2

x = x / 2

%=

Modulus Assignment

x %= 3

x = x % 3

Advance Compound Assignments:

Operator

Name

What it does

Code Example

Equivalent to...

&&=

Logical AND Assignment

Only assign if the variable is already true (or has a value)

x &&= 10

if (x) { x = 10 }

||=

Logical OR Assignment

Only assign if the variable is falsy.

x ||= 10

x || (x = 10)

??=

Nullish Coalescing Assignment

Only assign if the variable is null or undefined.

x ??= 5

if (x == null) { x = 5 }

Comparison Operators

These compare two values and return a Boolean (true or false). They are the backbone of decision-making in code.

In JavaScript, we have two ways to check if things are equal.

Loose Equality (==):

It is like comparing a 5 (text) to a 5 (number).

JS says "same same!" and calls it true.

Strict Equality (===):

This is much stricter.

It asks, "Are the values the same and are the types the same?"

Under this rule, a "5" (text) is NOT the same as a 5 (number).

JS says "same same but different!"

Operator

Name

What it does

Example

Result

==

Equal to

true if values are same (ignores type).

5 == "5"

true

===

Strict Equal

true if values and types are same.

5 === "5"

false

!=

Not Equal

true if values are different (ignores type).

5 != "5"

false

!==

Strict Not Equal

true if values or types are different.

5 !== "5"

true

>

Greater Than

true if left side is bigger.

10 > 5

true

<

Less Than

true if left side is smaller.

3 < 8

true

>=

Greater or Equal

true if bigger or exactly the same.

5 >= 5

true

<=

Less or Equal

true if smaller or exactly the same.

4 <= 2

false

Logical Operators

Logical operators allow you to build complex logic by combining multiple "True/False" comparisons together.

AND (&&)

It only returns true if every single condition being checked is true. If even one part is false, the whole thing fails.

Example:

To enter a high-security building, you must have both your ID card AND your fingerprint scanned. If either is missing, you stay outside.

You can write it like, if (idCard && fingerPrintSuccess) { ... }

OR (||)

It returns true if at least one of the conditions is true. It only fails if everything is false.

Example:

To pay for a coffee, you can use either your UPI or pay cash. As long as you have one, you get your coffee.

You can write it like, if (cash || upi) { ... }

NOT (!)

The NOT operator is a "reverser." It takes a value and flips it to the opposite state.

Example:

I don't love you.

You can write it like, if (!love) { ... }

Logical Truth Table

This table shows how the computer processes these combinations.

AND (&&)

A B A AND B
true true true
true false false
false true false
false false false

OR (||)

A B A OR B
true true true
true false true
false true true
false false false

NOT (!)

A !A
true false
false true

Short-Circuit Evaluation

  • In an && operation, if the first part is false, the computer stops immediately because it knows the whole thing can never be true.

  • In an || operation, if the first part is true, it stops because it knows the whole thing is already true.

This is incredibly useful for preventing errors. For example: if (user && user.name) The computer checks if user exists first. If it doesn't, it stops before trying to read user.name, preventing your app from crashing!

Operand

The values used by an operator are known as operands.

Ternary Operator (? :)

The ternary operator is a short form of if...else. Since this operator requires three operands, it is known as a ternary operator.

syntax

condition ? valueIfTrue : valueIfFalse

Example

// With Ternarily operator 
let age = 18;
let result = age >= 18 ? "Adult" : "Minor";

// Equivalent to
let result;
if (age >= 18) {
  result = "Adult";
} else {
  result = "Minor";
}

Nested Ternary statement

let score = 85;

let grade =
  score >= 90 ? "A" :
  score >= 75 ? "B" :
  score >= 50 ? "C" :
  "Fail";

As this operators require 3 operand so known as ternary operator.

Binary Operators

If an operator has two operands, it is known as a binary operator. Arithmetic, Comparison, Logical, and Assignment operators are examples of binary operators.

Unary Operators

If an operator has one operand, it is known as a unary operator.

Examples,

  • Logical Negation (!): Flips a true value to false or vice versa (e.g., !isTrue).

  • Increment/Decrement (++, --): Adds or subtracts one from a variable (e.g., i++).

  • Unary Plus/Minus (+, -): Indicates a positive or negative number (e.g., -5).

Precedence

Whether you were the topper of your class or a backbencher, you probably wondered at some point if the order of things really mattered. In life, maybe not always but in programming, order is everything.

Let’s start with a simple question.

What will this return?

console.log(10 + 5 * 2);

Take a second. Is it 30 or 20?

If you guessed 30, you’re thinking left to right:

(10 + 5) * 2 = 30

But JavaScript doesn’t think that way.

The actual answer is:

10 + (5 * 2) = 20

Why? Because multiplication has higher precedence than addition.

Why Operator Precedence Matters

Operator precedence decides the order in which operations are executed in an expression.

If you don’t understand it:

  • Your calculations may silently give wrong results.

  • Logical conditions may behave unexpectedly.

Now imagine this:

console.log(true || false && false);

What’s the result?

Many developers assume it evaluates left to right:

(true || false) && false

But that’s not how JavaScript evaluates it.

Logical AND (&&) has higher precedence than Logical OR (||).

So it actually becomes:

true || (false && false)
true || false
true

Subtle difference. Big impact.

The Rule of Thumb

JavaScript follows a predefined priority system called operator precedence.

And whenever you’re unsure use parentheses () to make your intention explicit.

console.log((10 + 5) * 2); // 30

Clear. Predictable. Safe.

Keep experimenting, keep breaking things, and most importantly, keep coding. The more you use these operators, the more natural they will feel in your daily development journey.

"In programming, the hard part isn't solving problems, but deciding what problems to solve."
Paul Graham

Let's Connect! 🚀

I’m currently deep-diving into the JavaScript, building projects and exploring the internals of the web. If you're on a similar journey or just love talking about JavaScript, let’s stay in touch!

Keep coding and keep building.

JavaScript

Part 23 of 24

Master JavaScript from the ground up with a structured, easy-to-follow learning path designed for serious developers. This series starts with core fundamentals like variables, data types, and control flow, then gradually moves into deeper topics such as functions, scope, prototypes, asynchronous programming, and how the JavaScript engine works internally. Each lesson focuses on clarity and real understanding. You will learn not only how to write JavaScript, but why it behaves the way it does. Concepts are explained using simple examples, practical use cases, and clean coding patterns that reflect real-world development.

Up next

Variables & Types: The First Principles of JavaScript

A guide to JavaScript's variable and data type system without losing your mind.