JavaScript Operators
Learn JavaScript operators with practical examples. Understand arithmetic, assignment, comparison, logical, and increment/decrement operators used in everyday JavaScript programming.
Reading Time
18 min
Lesson
Lesson 7 of 48
What are Operators?
Operators are special symbols that perform operations on values and variables. They allow you to calculate values, compare data, assign variables, and evaluate logical conditions. Operators are one of the most commonly used features in JavaScript.
Why are Operators Important?
Without operators, JavaScript cannot perform calculations or make decisions. Whether you're creating a calculator, validating forms, or checking conditions, operators are used everywhere.
Types of JavaScript Operators
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Increment & Decrement Operators
- String Operators
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
Arithmetic Operators Table
| Operator | Description | Example |
|---|---|---|
| + | Addition | 5 + 2 |
| - | Subtraction | 5 - 2 |
| * | Multiplication | 5 * 2 |
| / | Division | 10 / 2 |
| % | Remainder | 10 % 3 |
| ** | Exponentiation | 2 ** 3 |
Arithmetic Operators Example
Console Output
Click “Run” to see the console output here.
Assignment Operators
Assignment operators assign or update values stored in variables.
Assignment Operators Table
| Operator | Meaning |
|---|---|
| = | Assign |
| += | Add and Assign |
| -= | Subtract and Assign |
| *= | Multiply and Assign |
| /= | Divide and Assign |
| %= | Modulus and Assign |
Assignment Operator Example
Console Output
Click “Run” to see the console output here.
Comparison Operators
Comparison operators compare two values and always return either true or false.
Comparison Operators Table
| Operator | Description |
|---|---|
| == | Equal |
| === | Strict Equal |
| != | Not Equal |
| !== | Strict Not Equal |
| > | Greater Than |
| < | Less Than |
| >= | Greater Than or Equal |
| <= | Less Than or Equal |
Comparison Example
Console Output
Click “Run” to see the console output here.
Remember
Always prefer === instead of == because === compares both value and data type.
Logical Operators
Logical operators are used to combine multiple conditions.
Logical Operators Table
| Operator | Description |
|---|---|
| && | AND |
| || | OR |
| ! | NOT |
Logical Operator Example
Console Output
Click “Run” to see the console output here.
Increment & Decrement
Increment (++) increases a value by one, while decrement (--) decreases a value by one.
Increment Example
Console Output
Click “Run” to see the console output here.
Prefix vs Postfix
The prefix operator (++value) increments the value before it is used, while the postfix operator (value++) uses the current value first and increments it afterward.
Prefix vs Postfix Example
Console Output
Click “Run” to see the console output here.
String Concatenation Operator
The + operator is also used to join strings together.
String Example
Console Output
Click “Run” to see the console output here.
Common Mistake
The + operator joins strings, while -, *, and / automatically convert strings into numbers when possible.
Example
Console Output
Click “Run” to see the console output here.
Best Practice
Use strict equality (===) for comparisons and use parentheses to make complex expressions easier to read.
Summary
JavaScript provides different categories of operators for arithmetic calculations, assignments, comparisons, logical operations, and string concatenation. Mastering these operators is essential before learning conditionals and loops.