Mastering JavaScript Operators

Explore JavaScript operators: arithmetic for math, comparison for decisions, and logical for complex conditions. An interactive guide to the building blocks of JS logic.

Lesson ProgressStep 1 of 10
let x = 10 + 5;let y = 10 + 5 * 2;'5' === 5;(age > 18) && (hasLicense === true);(day === "Sat") || (day === "Sun");let msg = (score > 50) ? 'Pass' : 'Fail';
0 EXP

Welcome! Let's explore JavaScript operators, the symbols that perform actions in your code.

// Operators are the "verbs" of JavaScript...

Arithmetic Operators

These operators perform mathematical calculations. They are the foundation of any data manipulation.

  • + (Addition)
  • - (Subtraction)
  • * (Multiplication)
  • / (Division)
  • % (Modulus - Remainder)
  • ** (Exponentiation)
  • ++ (Increment), -- (Decrement)

System Check

What is the result of `10 + 5 * 2`?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🧮
Calculation Master

Correctly use arithmetic operators to solve a problem.

⚖️
Comparison Whiz

Correctly categorize comparison and logical operators.

🧠
Logic Expert

Build a valid conditional statement with multiple operators.

Mission: Perform a Calculation

Given `let x = 10;` and `let y = 5;`, create a new variable `z` that holds their sum. Our AI assistant will provide real-time feedback.

A.D.A. Feedback:

> System integrity looks stable. Code is valid.

Challenge: Categorize the Operators

Drag the operators from the list into their correct category box.

===
%
&&
>=
*
||

arithmetic

comparison

logical

Challenge: Complete the Logic

Fill in the missing operators to check if `age` is 18 or over, `AND` `isStudent` is `NOT` true.

let age = 20;let isStudent = false;if (age18(isStudent) ) { ... }

Consult A.D.A.

Community Holo-Net

Peer Project Review

Submit your "Calculator" project for feedback from other Net-Runners.

The Logic Engine: Operators in Practice

In JavaScript, operators are not just for math. They are the fundamental "verbs" of the language, allowing you to manipulate data, compare values, and make decisions. Mastering them is the key to moving from static content to dynamic, interactive applications.

1. Arithmetic Operators: The Calculators

These are the most straightforward, used for mathematical calculations. They include addition (`+`), subtraction (`-`), multiplication (`*`), division (`/`), modulus (`%`), and exponentiation (`**`).

  • Modulus (`%`): Finds the remainder. Incredibly useful for tasks like finding if a number is even or odd (`number % 2 === 0`).
  • Increment/Decrement (`++`, `--`): A common way to change counters in loops. `i++` is shorthand for `i = i + 1`.
  • Special Cases: Be aware of `Infinity` (e.g., `5 / 0`) and `NaN` (Not-a-Number, e.g., `0 / 0` or `'text' * 2`).

2. Assignment Operators: The Data Movers

The simple assignment operator (`=`) sets a variable's value. But **compound assignment operators** are powerful shortcuts for modifying a variable's existing value:

let score = 100;

// Instead of: score = score + 10;
score += 10; // score is now 110

let items = 20;
items -= 5; // items is now 15

let message = "Hello";
message += ", world!"; // message is "Hello, world!"

3. Comparison Operators: The Core of All Logic

This is arguably the most critical and bug-prone set of operators. They compare two values and return a boolean (`true` or `false`).

The Golden Rule: Strict (`===`) vs. Loose (`==`)

This single concept is vital for all JavaScript developers.

  • Loose Equality (`==`): Compares values *after* performing **type coercion**. It tries to "help" by converting types to match. This is unpredictable and dangerous.
  • Strict Equality (`===`): Compares both the **value AND the type**. If types are different, it's `false`. No exceptions.

✔️ Good Practice (Strict)

5 === 5;    // true
'5' === 5;  // false (string vs num)
0 === false; // false (num vs bool)

Predictable and safe.

❌ Bad Practice (Loose)

5 == 5;     // true
'5' == 5;   // true (string coerced)
0 == false;  // true (bool coerced)

Unpredictable and buggy.

**Rule:** Always use `===` and `!==` (Strict Inequality) unless you have a very specific, documented reason to use `==`.

4. Logical Operators: The Condition Combiners

Logical operators combine multiple boolean expressions into one.

  • `&&` (AND): Returns `true` only if **both** sides are true.
  • `||` (OR): Returns `true` if **at least one** side is true.
  • `!` (NOT): Flips a boolean value (`!true` becomes `false`).

Short-Circuiting & Falsy Values

Logical operators don't always return `true` or `false`. They "short-circuit" and return the value that *stopped* the evaluation.

// '&&' returns the first falsy value, or the last value
let x = 'Cat' && 'Dog'; // 'Dog' (last value, as both are truthy)
let y = 0 && 'Bird';   // 0 (0 is falsy, so it stops)

// '||' returns the first truthy value, or the last value
let z = null || 'Default'; // 'Default' (null is falsy, moves on)
let w = 'Admin' || 'User'; // 'Admin' ('Admin' is truthy, stops)

This is only possible because JavaScript has **falsy** values. The six falsy values are: `false`, `0`, `""` (empty string), `null`, `undefined`, and `NaN`. Everything else is **truthy**.

5. Other Key Operators

  • Ternary Operator (`? :`): A clean, one-line `if/else` statement.
    let theme = isDark ? 'dark' : 'light';
    // (condition) ? (value if true) : (value if false)
  • `typeof` Operator: Returns a string indicating the type of a variable (e.g., `"number"`, `"string"`, `"object"`, `"function"`).

6. Operator Precedence

Why does `10 + 5 * 2` equal `20` and not `30`? **Precedence.** JavaScript has a built-in order, just like PEMDAS in math. Multiplication (`*`) has a higher precedence than addition (`+`).

When in doubt, don't memorize the whole table. Just use **parentheses `()`** to control the order explicitly. They have the highest precedence.

let result1 = 10 + 5 * 2; // 20
let result2 = (10 + 5) * 2; // 30
Key Takeaway: Write clean, predictable code. Always use `===`. Use `()` to clarify precedence. Understand truthy/falsy values to master logical operators. This is the foundation of all JavaScript logic.

JavaScript Operator Glossary

Expression
Any unit of code that resolves to a value. `5`, `10 + 5`, and `isLoggedIn` are all expressions.
Operator
A special symbol that performs an operation on one or more values (operands) and produces a result. Example: In `10 + 5`, `+` is the operator.
Operand
The value (or expression) that an operator acts upon. In `10 + 5`, `10` and `5` are operands.
Arithmetic Operators
Perform mathematical calculations.
`+` (Add), `-` (Subtract), `*` (Multiply), `/` (Divide), `%` (Modulus/Remainder), `**` (Exponentiation), `++` (Increment), `--` (Decrement).
Assignment Operators
Assign a value to a variable.
`=` (Assign), `+=` (Add & Assign), `-=` (Subtract & Assign), `*=` (Multiply & Assign), etc.
Comparison Operators
Compare two values and return a boolean (`true`/`false`).
`==` (Loose Equals), `===` (Strict Equals), `!=` (Loose Not Equals), `!==` (Strict Not Equals), `>` (Greater), `<` (Less), `>=` (Greater/Equal), `<=` (Less/Equal).
Logical Operators
Combine or invert boolean expressions.
`&&` (Logical AND), `||` (Logical OR), `!` (Logical NOT).
Ternary Operator
`condition ? exprIfTrue : exprIfFalse`
The only JavaScript operator that takes three operands. A compact `if-else` statement.
Unary Operator
An operator that acts on a single operand.
Examples: `!` (Logical NOT), `typeof`, `delete`, `++` (Increment).
Type Coercion
The automatic conversion of a value from one data type to another. This is done by the loose equality `==` operator and is a common source of bugs.
Operator Precedence
The order in which operators are evaluated. For example, `*` has higher precedence than `+`. Use parentheses `()` to override this order.
Short-Circuiting
A behavior of logical operators (`&&`, `||`) where the second operand is not evaluated if the first operand is sufficient to determine the result.
Truthy / Falsy
In a boolean context (like an `if` statement), all values are considered "truthy" except for the 6 **falsy** values: `false`, `0`, `""`, `null`, `undefined`, and `NaN`.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching JavaScript and building robust, efficient web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest ES2025 specifications and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!