What are Operators in JavaScript?
Operators in JavaScript are symbols used to carry out operations on variables and values. They allow you to manipulate data, perform calculations, and make decisions within your code. Operators work with operands (values or variables) to produce a result.
Types of Operators in JavaScript
- Arithmetic Operators – Perform basic mathematical operations.
- Assignment Operators – Assign values to variables.
- Comparison Operators – Evaluate two values and return a Boolean result (true or false).
- Logical Operators – Carry out logical operations (AND, OR, NOT).
- String Operators – Concatenate (join) strings.
- Bitwise Operators – Carry out operations at the binary level.
- Ternary (Conditional) Operator – A shorthand for
if-elseconditions. - Type Operators – Determine or manipulate the type of a value.
- Increment/Decrement Operators – Increase or decrease a value by 1.
How to Use Operators in JavaScript
Operators are used in expressions to perform calculations, compare values, and control program flow.
1. Arithmetic Operators
Perform mathematical calculations.
| Operator | Description | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 6 | 4 |
* | Multiplication | 4 * 2 | 8 |
/ | Division | 8 / 2 | 4 |
% | Modulus (Remainder) | 10 % 3 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
Example:
2. Assignment Operators
Assign values to variables.
| Operator | Description | Example | Equivalent To |
|---|---|---|---|
= | Assign | x = 10 | x = 10 |
+= | Add or assign | x += 5 | x = x + 5 |
-= | Subtract or assign | x -= 3 | x = x - 3 |
*= | Multiply or assign | x *= 2 | x = x * 2 |
/= | Divide or assign | x /= 4 | x = x / 4 |
%= | Modulus or assign | x %= 2 | x = x % 2 |
Example:
3. Comparison Operators
Compare two values and return true or false.
| Operator | Description | Example | Result |
|---|---|---|---|
== | Equal to | 5 == "5" | true |
=== | Strict equal (type + value) | 5 === "5" | false |
!= | Not equal | 5 != 3 | true |
!== | Strict not equal | 5 !== "5" | true |
> | Greater than | 10 > 5 | true |
< | Less than | 3 < 7 | true |
>= | Greater than or equal | 8 >= 8 | true |
<= | Less than or equal | 6 <= 5 | false |
Example:
4. Logical Operators
Combine multiple conditions.
| Operator | Description | Example | Result |
|---|---|---|---|
&& | Logical AND | true && false | false |
| ` | ` | Logical OR | |
! | Logical NOT | !true | false |
Example:
5. String Operators
Used to concatenate (join) strings.
6. Ternary Operator
A shorthand for if-else statements.
7. Increment/Decrement Operators
Increase or decrease a variable by 1.
| Operator | Description | Example | Result |
|---|---|---|---|
++ | Increment | x++ | x + 1 |
-- | Decrement | x-- | x - 1 |
Example:
Why Use Operators?
- Perform Calculations – For mathematical operations.
- Make Decisions – Use comparison and logical operators to control program flow.
- Manipulate Data – Update, assign, and transform data easily.
- Shorter Code – Reduce the need for lengthy if-else statements.
When to Use Operators?
- Arithmetic Operations – For performing calculations.
- Conditional Checks – In
ifstatements and loops. - String Concatenation – To build messages or form outputs dynamically.
- Boolean Logic – To handle multiple conditions efficiently.
Example: Combining Operators