Computer ScienceChapter 3: Programming Fundamentals
3.4.3 Operators
Operators:
Explanation level
This topic explains the arithmetic operators used in JavaScript to perform mathematical calculations on variables and values.
Definition
Operators: Operators are special symbols used to perform operations on variables and values. JavaScript supports arithmetic operators for addition, subtraction, multiplication, division, and modulus.
Core Concept
Operators are like the buttons on a calculator. Just as you press "+" to add, "-" to subtract, "×" to multiply, and "÷" to divide, JavaScript uses symbols like `+`, `-`, `*`, and `/` to perform calculations. The modulus operator (`%`) is like finding the remainder after division—for example, 7 % 3 gives 1 because 3 goes into 7 twice with 1 left over. Operators let you manipulate numbers and variables to produce new results.
Detailed Explanation
Arithmetic Operators in JavaScript
JavaScript supports arithmetic operators to be used:
| Operator | Name | Description | Example |
|---|---|---|---|
| `+` | Addition | Adds two values | `a + b` |
| `-` | Subtraction | Subtracts one value from another | `a - b` |
| `*` | Multiplication | Multiplies two values | `a * b` |
| `/` | Division | Divides one value by another | `ab` |
| `%` | Modulus | Gives the remainder of a division | `a % b` |
Example: Handling and Printing Multiple Variables
The code shown below takes 3 variables namely, 'a', 'b' and 'reward'. Only `reward` is initialized through a constant value in line 5. In lines 6 and 7, the remainder of `reward` divided by 10 is stored in 'a', and 'b' is assigned the value when `reward` is divided by 10, respectively.
```html <html> <body> <script type="text/javascript"> var a, b, reward; reward = 5555; a = reward % 10; b = reward / 10; document.write("Value of variable a is:", a); document.write("<br />") document.write("Value of variable b is:", b); </script> </body> </html> ```
Output: Value of variable a is: 5 Value of variable b is: 555.5
- `reward = 5555;` initializes `reward` with 5555.
- 0 calculates 5555 % 10 = 5, assigns to 1.
- 0 calculates 555510 = 555.5, assigns to 1.
- `document.write()` displays the values.
Understanding the Modulus Operator (%)
The modulus operator gives the remainder after division.
Examples:
| Expression | Result | Explanation |
|---|---|---|
| `10 % 3` | 1 | 3 goes into 10 three times (9), remainder 1 |
| `15 % 4` | 3 | 4 goes into 15 three times (12), remainder 3 |
| `20 % 5` | 0 | 5 goes into 20 four times (20), remainder 0 |
| `7 % 2` | 1 | 2 goes into 7 three times (6), remainder 1 |
- Checking if a number is even or odd (`num % 2 == 0` means even).
- Extracting digits from a number (e.g., last digit = `num % 10`).
- Cyclic operations (e.g., wrapping around an array).
Order of Operations (Operator Precedence)
- Parentheses `()`
- Multiplication `*`, Division `/`, Modulus `%` (left to right)
- Addition `+`, Subtraction `-` (left to right)
Example: ```javascript var result = 5 + 3 * 2; // 3 * 2 = 6, then 5 + 6 = 11 var result2 = (5 + 3) * 2; // 5 + 3 = 8, then 8 * 2 = 16 ```
Summary of Operators
| Operator | Symbol | Example | Result |
|---|---|---|---|
| Addition | `+` | `5 + 3` | 8 |
| Subtraction | `-` | `5 - 3` | 2 |
| Multiplication | `*` | `5 * 3` | 15 |
| Division | `/` | `63` | 2 |
| Modulus | `%` | `5 % 3` | 2 |
Intermediate Level
JavaScript supports five arithmetic operators: addition (`+`), subtraction (`-`), multiplication (`*`), division (`/`), and modulus (`%`). The modulus operator returns the remainder of a division—useful for checking even/odd numbers or extracting digits. Operators follow standard precedence: parentheses first, then multiplication/division/modulus, then addition/subtraction. Variables can be used with operators to perform calculations and display results using `document.write()`. Understanding operators is essential for building calculators, games, and data processing scripts.
Important Points
- - Operators: `+`, `-`, `*`, `/`, `%`.
- - Modulus (`%`) gives the remainder.
- - `reward % 10` gives the last digit.
- - `reward / 10` removes the last digit (as a decimal).
- - Order of operations: parentheses → `*`, `/`, `%` → `+`, `-`.
- - Operators can be used with variables.
- - Results can be displayed with `document.write()`.
Remember
- - Addition: `+`
- - Subtraction: `-`
- - Multiplication: `*`
- - Division: `/`
- - Modulus: `%` (remainder)
- - Order: Parentheses → ×, ÷, % → +, −
- - Example: `5555 % 10 = 5`
- - Example: `555510 = 555.5`
Common Mistakes
- - Confusing `%` with percentage: In JavaScript, `%` is modulus (remainder), not percent.
- - Forgetting operator precedence: Use parentheses to clarify.
- - Mixing types: `"5" + 3` gives `"53"`, not `8`.
- - Division by zero: Returns `Infinity` or `NaN`.
- - Using `=` instead of `==`: `=` assigns; `==` compares.
Quick Revision
JavaScript supports arithmetic operators: addition (`+`), subtraction (`-`), multiplication (`*`), division (`/`), and modulus (`%`). The modulus operator gives the remainder of a division—useful for extracting digits or checking even/odd. Operators follow standard precedence: parentheses first, then multiplication/division/modulus, then addition/subtraction. Variables can be used with operators, and results can be displayed using `document.write()`.
60-Second Revision
- `+` Addition
- `-` Subtraction
- `*` Multiplication
- `/` Division
- `%` Modulus (remainder)
Did you understand this topic?
Your answer is saved with your learning record. It does not mark the topic as mastered.
Quick revision
Revision Zone
Review the key facts, formulas and checks before you move on.3.4.3 Operators
Sticky Notes · Quick Revision
Key Point 1
- Operators: `+`, `-`, `*`, `/`, `%`.
Key Point 2
- **Modulus:** 0 = remainder.
Key Point 3
- Example: `5555 % 10 = 5`.
Key Point 4
- Example: `555510 = 555.5`.
Key Point 5
- Precedence: Parentheses → ×, ÷, % → +, −.
Key Point 6
- Output: `document.write()`.
Same supplied content as this Notes page (shared canonical source).