# JavaScript Operators

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.

<table style="min-width: 100px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><td colspan="1" rowspan="1"><p><strong>Operator</strong></p></td><td colspan="1" rowspan="1"><p><strong>Name</strong></p></td><td colspan="1" rowspan="1"><p><strong>Example</strong></p></td><td colspan="1" rowspan="1"><p><strong>Result</strong></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>+</code></p></td><td colspan="1" rowspan="1"><p>Addition</p></td><td colspan="1" rowspan="1"><p><code>10 + 5</code></p></td><td colspan="1" rowspan="1"><p><code>15</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>-</code></p></td><td colspan="1" rowspan="1"><p>Subtraction</p></td><td colspan="1" rowspan="1"><p><code>10 - 5</code></p></td><td colspan="1" rowspan="1"><p><code>5</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>*</code></p></td><td colspan="1" rowspan="1"><p>Multiplication</p></td><td colspan="1" rowspan="1"><p><code>10 * 5</code></p></td><td colspan="1" rowspan="1"><p><code>50</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>/</code></p></td><td colspan="1" rowspan="1"><p>Division</p></td><td colspan="1" rowspan="1"><p><code>10 / 4</code></p></td><td colspan="1" rowspan="1"><p><code>2.5</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>%</code></p></td><td colspan="1" rowspan="1"><p>Modulus</p></td><td colspan="1" rowspan="1"><p><code>10 % 3</code></p></td><td colspan="1" rowspan="1"><p><code>1</code> (remainder)</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>**</code></p></td><td colspan="1" rowspan="1"><p>Exponentiation</p></td><td colspan="1" rowspan="1"><p><code>2 ** 3</code> (cube of 2)</p></td><td colspan="1" rowspan="1"><p><code>8</code></p></td></tr></tbody></table>

## 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:**

<table style="min-width: 100px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><td colspan="1" rowspan="1"><p><strong>Operator</strong></p></td><td colspan="1" rowspan="1"><p><strong>Name</strong></p></td><td colspan="1" rowspan="1"><p><strong>Example</strong></p></td><td colspan="1" rowspan="1"><p><strong>Equivalent to</strong></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>+=</code></p></td><td colspan="1" rowspan="1"><p>Addition Assignment</p></td><td colspan="1" rowspan="1"><p><code>x += 5</code></p></td><td colspan="1" rowspan="1"><p><code>x = x + 5</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>-=</code></p></td><td colspan="1" rowspan="1"><p>Subtraction Assignment</p></td><td colspan="1" rowspan="1"><p><code>x -= 2</code></p></td><td colspan="1" rowspan="1"><p><code>x = x - 2</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>*=</code></p></td><td colspan="1" rowspan="1"><p>Multiplication Assignment</p></td><td colspan="1" rowspan="1"><p><code>x *= 3</code></p></td><td colspan="1" rowspan="1"><p><code>x = x * 3</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>/=</code></p></td><td colspan="1" rowspan="1"><p>Division Assignment</p></td><td colspan="1" rowspan="1"><p><code>x /= 2</code></p></td><td colspan="1" rowspan="1"><p><code>x = x / 2</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>%=</code></p></td><td colspan="1" rowspan="1"><p>Modulus Assignment</p></td><td colspan="1" rowspan="1"><p><code>x %= 3</code></p></td><td colspan="1" rowspan="1"><p><code>x = x % 3</code></p></td></tr></tbody></table>

**Advance Compound Assignments:**

<table style="min-width: 125px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><td colspan="1" rowspan="1"><p><strong>Operator</strong></p></td><td colspan="1" rowspan="1"><p><strong>Name</strong></p></td><td colspan="1" rowspan="1"><p><strong>What it does</strong></p></td><td colspan="1" rowspan="1"><p><strong>Code Example</strong></p></td><td colspan="1" rowspan="1"><p><strong>Equivalent to...</strong></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>&amp;&amp;=</code></p></td><td colspan="1" rowspan="1"><p><strong>Logical AND Assignment</strong></p></td><td colspan="1" rowspan="1"><p>Only assign if the variable is already true (or has a value)</p></td><td colspan="1" rowspan="1"><p><code>x &amp;&amp;= 10</code></p></td><td colspan="1" rowspan="1"><p><code>if (x) { x = 10 }</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>||=</code></p></td><td colspan="1" rowspan="1"><p><strong>Logical OR Assignment</strong></p></td><td colspan="1" rowspan="1"><p>Only assign if the variable is falsy.</p></td><td colspan="1" rowspan="1"><p><code>x ||= 10</code></p></td><td colspan="1" rowspan="1"><p>x || (x = 10)</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>??=</code></p></td><td colspan="1" rowspan="1"><p><strong>Nullish Coalescing Assignment</strong></p></td><td colspan="1" rowspan="1"><p>Only assign if the variable is <strong>null or undefined</strong>.</p></td><td colspan="1" rowspan="1"><p><code>x ??= 5</code></p></td><td colspan="1" rowspan="1"><p><code>if (x == null) { x = 5 }</code></p></td></tr></tbody></table>

## 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!"

<table style="min-width: 125px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><td colspan="1" rowspan="1"><p><strong>Operator</strong></p></td><td colspan="1" rowspan="1"><p><strong>Name</strong></p></td><td colspan="1" rowspan="1"><p><strong>What it does</strong></p></td><td colspan="1" rowspan="1"><p><strong>Example</strong></p></td><td colspan="1" rowspan="1"><p><strong>Result</strong></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>==</code></p></td><td colspan="1" rowspan="1"><p><strong>Equal to</strong></p></td><td colspan="1" rowspan="1"><p><code>true</code> if values are same (ignores type).</p></td><td colspan="1" rowspan="1"><p><code>5 == "5"</code></p></td><td colspan="1" rowspan="1"><p><code>true</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>===</code></p></td><td colspan="1" rowspan="1"><p><strong>Strict Equal</strong></p></td><td colspan="1" rowspan="1"><p><code>true</code> if values <strong>and</strong> types are same.</p></td><td colspan="1" rowspan="1"><p><code>5 === "5"</code></p></td><td colspan="1" rowspan="1"><p><code>false</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>!=</code></p></td><td colspan="1" rowspan="1"><p><strong>Not Equal</strong></p></td><td colspan="1" rowspan="1"><p><code>true</code> if values are different (ignores type).</p></td><td colspan="1" rowspan="1"><p><code>5 != "5"</code></p></td><td colspan="1" rowspan="1"><p><code>false</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>!==</code></p></td><td colspan="1" rowspan="1"><p><strong>Strict Not Equal</strong></p></td><td colspan="1" rowspan="1"><p><code>true</code> if values <strong>or</strong> types are different.</p></td><td colspan="1" rowspan="1"><p><code>5 !== "5"</code></p></td><td colspan="1" rowspan="1"><p><code>true</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>&gt;</code></p></td><td colspan="1" rowspan="1"><p><strong>Greater Than</strong></p></td><td colspan="1" rowspan="1"><p><code>true</code> if left side is bigger.</p></td><td colspan="1" rowspan="1"><p><code>10 &gt; 5</code></p></td><td colspan="1" rowspan="1"><p><code>true</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>&lt;</code></p></td><td colspan="1" rowspan="1"><p><strong>Less Than</strong></p></td><td colspan="1" rowspan="1"><p><code>true</code> if left side is smaller.</p></td><td colspan="1" rowspan="1"><p><code>3 &lt; 8</code></p></td><td colspan="1" rowspan="1"><p><code>true</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>&gt;=</code></p></td><td colspan="1" rowspan="1"><p><strong>Greater or Equal</strong></p></td><td colspan="1" rowspan="1"><p><code>true</code> if bigger or exactly the same.</p></td><td colspan="1" rowspan="1"><p><code>5 &gt;= 5</code></p></td><td colspan="1" rowspan="1"><p><code>true</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>&lt;=</code></p></td><td colspan="1" rowspan="1"><p><strong>Less or Equal</strong></p></td><td colspan="1" rowspan="1"><p><code>true</code> if smaller or exactly the same.</p></td><td colspan="1" rowspan="1"><p><code>4 &lt;= 2</code></p></td><td colspan="1" rowspan="1"><p><code>false</code></p></td></tr></tbody></table>

## 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**

```javascript
condition ? valueIfTrue : valueIfFalse
```

**Example**

```javascript
// 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**

```javascript
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?

```javascript
console.log(10 + 5 * 2);
```

Take a second. Is it 30 or 20?

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

```javascript
(10 + 5) * 2 = 30
```

But JavaScript doesn’t think that way.

The actual answer is:

```javascript
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:

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

What’s the result?

Many developers assume it evaluates left to right:

```javascript
(true || false) && false
```

But that’s not how JavaScript evaluates it.

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

So it actually becomes:

```javascript
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.

```javascript
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!

*   **Connect on LinkedIn:** [**Satpalsinh's Profile**](https://www.linkedin.com/in/satpalsinhrana)
    
*   **Follow my Blog:** [**blogs.satpal.cloud**](http://blogs.satpal.cloud)
    

**Keep coding and keep building.**
