Of course. Here are in-depth notes on JavaScript operators, structured for a student lecture. These notes cover the categories, their specific behaviors, common "gotchas," and best practices.
In JavaScript, an operator is a special symbol or keyword used to perform an operation on values. The values that the operator works on are called operands. The combination of operators and operands forms an expression, which evaluates to a single value.
// In this expression: 5 + 10
// -> `+` is the operator.
// -> `5` and `10` are the operands.
// -> The entire expression `5 + 10` evaluates to the value `15`.
let result = 5 + 10;
These operators are used to assign a value to a variable.
= (Assignment): The fundamental assignment operator. It assigns the value on its right to the variable on its left. code JavaScript
let score = 100;
Compound Assignment (Shorthands): These combine a mathematical operation with an assignment. They are very common and make code more concise.
| Operator | Example | Equivalent To |
|---|---|---|
| += | x += y | x = x + y |
| -= | x -= y | x = x - y |
| *= | x *= y | x = x * y |
| /= | x /= y | x = x / y |
| %= | x %= y | x = x % y |
| **= | x **= y | x = x ** y |
let level = 10;
level += 5; // level is now 15
level *= 2; // level is now 30
Used for standard mathematical calculations.