Introduction to Operators
Learn what JavaScript operators are, why we need them, and how to use arithmetic, assignment, comparison, logical, and other operator types in your code.
What is a JavaScript Operator?Link to this section
An operator is a special symbol used to perform operations on values and variables. Just like in mathematics, these symbols tell JavaScript to add numbers, compare text, or check conditions.
Here, the plus sign and the equal sign work together to calculate the final price.
- `+` is the arithmetic operator that performs addition.
- `=` is the assignment operator that stores the result.
- `subtotal`, `tax`, and `total` are the variables holding the data.
- `10`, `2`, and `12` are the number values being processed.
Operators form the backbone of any programming logic, turning static data into dynamic calculations.
Why Do We Need JavaScript Operators?Link to this section
Without operators, code would only be able to store values without ever changing, combining, or testing them.
- Calculate total shop bills by adding item prices.
- Check if a student passing mark is greater than or equal to 40.
- Verify user login credentials by comparing entered passwords.
- Calculate monthly salaries after deductions and bonuses.
- Determine if a user is old enough to access a website.
- Combine first and last names to display a full profile name.
We need operators to adjust prices, calculate taxes, and handle shopping cart totals.
The subtraction operator helps us quickly compute the discounted amount a customer has to pay.
About JavaScript OperatorsLink to this section
Operators take one or more inputs, called operands, and produce a new value. They are grouped by their specific tasks, such as math, comparison, or logic. Understanding how they interact ensures your programs run without unexpected bugs.
| Operator Category | Main Purpose | Common Symbols |
|---|---|---|
| Arithmetic | Perform math calculations | +, -, *, /, %, **, ++, -- |
| Assignment | Store values in variables | =, +=, -=, *=, /= |
| Comparison | Compare two values | ==, ===, !=, !==, >, <, >=, <= |
| Logical | Combine multiple conditions | &&, ||, ! |
Operators can modify existing variable values in place using shorthand syntax.
Types of JavaScript OperatorsLink to this section
1. Arithmetic OperatorsLink to this section
Arithmetic operators perform basic mathematical operations like addition, subtraction, multiplication, and division on numbers.
| Operator | Name | Example Result |
|---|---|---|
| + | Addition | 5 + 2 = 7 |
| - | Subtraction | 5 - 2 = 3 |
| * | Multiplication | 5 * 2 = 10 |
| / | Division | 6 / 2 = 3 |
| % | Remainder | 5 % 2 = 1 |
| ** | Exponentiation | 2 ** 3 = 8 |
note
warning
2. Assignment OperatorsLink to this section
Assignment operators assign values to variables. They can also combine an arithmetic operation with assignment for concise code.
| Operator | Same As | Example |
|---|---|---|
| x += y | x = x + y | Add and assign |
| x -= y | x = x - y | Subtract and assign |
| x *= y | x = x * y | Multiply and assign |
| x /= y | x = x / y | Divide and assign |
tip
3. Comparison OperatorsLink to this section
Comparison operators compare two values and return a boolean result, which is either true or false.
| Operator | Description | Example |
|---|---|---|
| === | Strict equal | 5 === 5 (true) |
| !== | Strict not equal | 5 !== 3 (true) |
| > | Greater than | 10 > 5 (true) |
| < | Less than | 3 < 8 (true) |
| >= | Greater than or equal | 5 >= 5 (true) |
| <= | Less than or equal | 4 <= 9 (true) |
warning
4. Logical OperatorsLink to this section
Logical operators combine multiple boolean expressions to make complex decisions in your code.
| Operator | Name | Rule |
|---|---|---|
| && | Logical AND | True if both sides are true |
| || | Logical OR | True if at least one side is true |
| ! | Logical NOT | Reverses true to false or false to true |
tip
5. Increment and Decrement OperatorsLink to this section
Increment and decrement operators increase or decrease a variable's value by exactly 1.
Post-increment: The current value is used first, then increased by one.
Pre-increment: The value is increased by one first, then used.
note
Operator PrecedenceLink to this section
Just like in regular math, JavaScript operators follow an order of operations. Multiplication and division happen before addition and subtraction.
tip
Complete Coding ExampleLink to this section
This complete program calculates a shopping cart total, applies a discount, and checks shipping eligibility.
This script brings together arithmetic, assignment, and comparison operators to process an online purchase.
Important NotesLink to this section
- Operators are symbols used to perform operations on values and variables.
- Arithmetic operators include +, -, *, /, %, and **.
- The assignment operator (=) stores values inside variables.
- Compound assignment operators like += combine math and assignment.
- Comparison operators evaluate two values and return true or false.
- Strict equality (===) checks both value and data type without conversion.
- Logical AND (&&) requires all conditions to be true.
- Logical OR (||) requires only one condition to be true.
- Increment (++) and decrement (--) change variable values by 1.
- Operator precedence determines the order in which parts of an expression are calculated.
- Parentheses can override standard operator precedence rules.
SummaryLink to this section
JavaScript operators give your code the power to calculate, compare, and make decisions. By combining arithmetic, comparison, and logical operators, you can build dynamic applications that handle real-world logic like shop bills, user logins, and scoring systems.
Check Your UnderstandingLink to this section
What is the output of `let x = 10 % 3; console.log(x);`?
Practice ChallengesLink to this section
1. Tip Calculator
Create two variables for a meal cost of 50 and a tip amount of 10. Use addition to calculate the final total bill and print it.
Expected output
60Hint: Use the + operator to add meal cost and tip.
2. Discounted Price
A jacket costs 120. Apply a 20 dollar discount using a subtraction assignment operator (-=) and print the new price.
Expected output
100Hint: Use jacketPrice -= 20;
3. Even or Odd Checker
Use a variable with the number 17 and the remainder operator (%) with 2 to check if the number has a remainder.
Expected output
1Hint: 17 % 2 will yield 1.
4. Pass or Fail Evaluator
Create a score variable with 75. Write a comparison using >= to check if the score is greater than or equal to 50, and print the boolean result.
Expected output
trueHint: Compare score >= 50
5. Complex Order Calculation
Calculate the expression (10 + 5) * 2 - 4 using operator precedence rules with parentheses and print the result.
Expected output
26Hint: Evaluate the parentheses first, then multiply, then subtract.
Rate this content
Sign in to rate
Discussion
Sign in to join the discussion