JavaScript Operators and Expressions
What Are Operators and Expressions?
In JavaScript, an expression is any valid combination of variables, values, and operators that results in a value.
An operator is a symbol that performs a specific operation on one or more values (called operands).
Example:
let sum = 10 + 5; // '+' is an operator; '10 + 5' is an expression
Arithmetic Operators
These are used to perform basic mathematical operations.
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 5 | 5 |
* | Multiplication | 10 * 5 | 50 |
/ | Division | 10 / 5 | 2 |
% | Modulus (remainder) | 10 % 3 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
++ | Increment | a++ | a = a + 1 |
-- | Decrement | a-- | a = a - 1 |
Example:
let x = 10;
x++; // 11
x *= 2; // 22
Assignment Operators
These assign values to variables.
Operator | Description | Example | Equivalent To |
---|---|---|---|
= | Assign | x = 10 | — |
+= | Add and assign | x += 5 | x = x + 5 |
-= | Subtract and assign | x -= 3 | x = x - 3 |
*= | Multiply and assign | x *= 2 | x = x * 2 |
/= | Divide and assign | x /= 2 | x = x / 2 |
%= | Modulus and assign | x %= 3 | x = x % 3 |
Example:
let a = 10;
a += 5; // a is now 15
Comparison Operators
These are used to compare two values and return a Boolean result (true
or false
).
Operator | Description | Example | Result |
---|---|---|---|
== | Equal (loose) | 5 == '5' | true |
=== | Equal (strict) | 5 === '5' | false |
!= | Not equal (loose) | 5 != '5' | false |
!== | Not equal (strict) | 5 !== '5' | true |
> | Greater than | 10 > 5 | true |
< | Less than | 5 < 10 | true |
>= | Greater than or equal to | 10 >= 10 | true |
<= | Less than or equal to | 10 <= 5 | false |
Tip:
Use ===
and !==
instead of ==
and !=
to avoid unexpected type coercion.
Logical Operators
Used to combine multiple boolean expressions.
Operator | Name | Example | Result |
---|---|---|---|
&& | Logical AND | true && false | false |
` | ` | Logical OR | |
! | Logical NOT | !true | false |
Example:
let age = 25;
let isAdult = age >= 18 && age < 60; // true
Bitwise Operators (Advanced, Use Rarely)
These work on numbers at the binary level.
Operator | Description | Example |
---|---|---|
& | AND | 5 & 1 // 1 |
` | ` | OR |
^ | XOR | 5 ^ 1 // 4 |
~ | NOT | ~5 // -6 |
<< | Left shift | 5 << 1 // 10 |
>> | Right shift | 5 >> 1 // 2 |
Note: Bitwise operators are rarely used in beginner-level JavaScript but useful in performance-critical or system-level programming.
Ternary Operator
A shorthand for if...else
.
Syntax:
condition ? expressionIfTrue : expressionIfFalse;
Example:
let age = 18;
let status = age >= 18 ? "Adult" : "Minor"; // "Adult"
This is very useful for quick value decisions.
Operator Precedence
When multiple operators appear in one expression, JavaScript follows a specific order of operations (just like math).
For example:
let result = 10 + 5 * 2; // 20 (not 30)
Explanation:
Multiplication (*
) has higher precedence than addition (+
), so 5 * 2
happens first.
Common Precedence Order:
()
— Parentheses**
— Exponentiation* / %
— Multiplication, Division, Modulus+ -
— Addition, Subtraction- Comparison (
<
,>
,==
, etc.) - Logical (
&&
,||
) - Assignment (
=
,+=
, etc.)
Use parentheses to control and clarify complex expressions:
let result = (10 + 5) * 2; // 30
Summary
- Use arithmetic operators for math (
+
,-
,*
,/
). - Use assignment operators (
=
,+=
,-=
) to set or update values. - Use comparison operators (
===
,>
,<
) to compare values. - Use logical operators (
&&
,||
,!
) in conditional expressions. - Use the ternary operator for quick
if...else
alternatives. - Know operator precedence to avoid bugs in complex expressions.