Skip to main content

Command Palette

Search for a command to run...

Operators & Expressions in JavaScript

Updated
4 min readView as Markdown

Yesterday, I learned how JavaScript stores information using variables and different data types.

Today, I explored Operators & Expressions, which allow JavaScript to perform calculations, compare values, and evaluate logical conditions.

Operators are the tools that make programming dynamic. Whether you're building a calculator, validating a login form, or checking user permissions, operators are used everywhere.

Let's dive in.


What is an Operator?

An operator is a special symbol that performs an operation on one or more values (called operands).

Example:

let result = 10 + 5;

Here:

  • 10 and 5 are operands.

  • + is the operator.

The result is:

15

What is an Expression?

An expression is any piece of code that produces a value.

Example:

10 + 5

This expression evaluates to 15.

Another example:

let total = price * quantity;

The multiplication is an expression whose result is assigned to total.


Types of Operators

JavaScript provides several categories of operators:

  • Arithmetic Operators

  • Assignment Operators

  • Comparison Operators

  • Logical Operators

  • Increment & Decrement Operators

  • Ternary Operator


1. Arithmetic Operators

Used for mathematical calculations.

Operator Description Example
+ Addition 5 + 2
- Subtraction 5 - 2
* Multiplication 5 * 2
/ Division 10 / 2
% Modulus 10 % 3
** Exponentiation 2 ** 3

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);
console.log(a ** b);

2. Assignment Operators

Used to assign or update values.

let x = 10;

x += 5;
x -= 2;
x *= 3;
x /= 2;

Examples:

Operator Meaning
= Assign
+= Add and assign
-= Subtract and assign
*= Multiply and assign
/= Divide and assign

3. Comparison Operators

Used to compare two values.

The result is always a Boolean (true or false).

let age = 18;

console.log(age > 16);

Common operators:

Operator Description
== Equal (loose)
=== Strict Equal
!= Not Equal
!== Strict Not Equal
> Greater Than
< Less Than
>= Greater Than or Equal
<= Less Than or Equal

Example:

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

Output:

false

=== compares both value and data type, making it safer than ==.


4. Logical Operators

Logical operators combine multiple conditions.

AND (&&)

Returns true only if both conditions are true.

console.log(true && false);

OR (||)

Returns true if at least one condition is true.

console.log(true || false);

NOT (!)

Reverses a Boolean value.

console.log(!true);

5. Increment & Decrement

Increase or decrease a value by 1.

let count = 5;

count++;

count--;

Useful inside loops and counters.


6. Ternary Operator

A shorthand way of writing an if...else statement.

Syntax:

condition ? valueIfTrue : valueIfFalse;

Example:

let age = 20;

let message = age >= 18 ? "Adult" : "Minor";

console.log(message);

Output:

Adult

Operator Precedence

JavaScript follows mathematical order when evaluating expressions.

Example:

console.log(2 + 3 * 4);

Output:

14

Multiplication happens before addition.

Use parentheses to control the order:

console.log((2 + 3) * 4);

Output:

20

Best Practices

✔ Prefer === over ==.

✔ Use meaningful expressions.

✔ Keep complex conditions readable.

✔ Use parentheses when needed for clarity.

✔ Understand operator precedence to avoid unexpected results.


My Biggest Takeaway

Today, I learned that operators are the building blocks of logic in JavaScript.

Whether it's performing calculations, comparing values, or making decisions, operators are used in almost every program.

Understanding the difference between arithmetic, comparison, logical, and assignment operators has given me a clearer picture of how JavaScript processes information.

1 views

100 Days of Code: My Journey to Becoming a Full Stack Developer

Part 33 of 50

Welcome to my 100 Days of Code journey! In this series, I'll document my daily progress as I learn Full Stack Web Development from the ground up. Every post will cover what I learned, challenges I faced, mistakes I made, and the projects I built. My goal is not just to complete 100 days but to become a better developer through consistency, discipline, and learning in public. Topics I'll cover include: • Git & GitHub • HTML, CSS & JavaScript • React.js • Node.js & Express • MongoDB • APIs • Real-world Projects • AI tools for Developers Whether you're just starting out or revising your fundamentals, I hope this journey helps you learn alongside me. Let's build, learn, and grow together! 🚀

Up next

Conditional Statements in JavaScript

Yesterday, I learned how JavaScript uses operators to perform calculations and compare values. Today, I explored Conditional Statements, which allow a program to make decisions based on different cond

More from this blog

T

TheSaurceCode

73 posts

Documenting my journey to becoming a Full Stack Developer through daily blogs, coding challenges, projects, tutorials, and lessons learned. Learn, build, and grow with me