Operators & Expressions in JavaScript
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:
10and5are 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.
