JavaScript Operators¶
What are Arithmetic Operators?¶
Arithmetic operators are used to perform mathematical calculations like addition, subtraction, and multiplication. They are the most basic building blocks of programming logic.
| Operator | Operation | Description |
|---|---|---|
+ |
Addition | Adds two values together |
- |
Subtraction | Subtracts the right value from the left value |
* |
Multiplication | Multiplies two values |
/ |
Division | Divides the left value by the right value |
% |
Modulus | Returns the remainder after division |
Note: While the basic behavior is the same across languages, how they handle things like "Integer Division" (e.g., 5 / 2) can vary.
Assignment Operators¶
Assignment operators assign values to JavaScript variables.
| Operator | Example | Same As |
|---|---|---|
= |
x = 10 |
x = 10 |
+= |
x += 5 |
x = x + 5 |
-= |
x -= 5 |
x = x - 5 |
*= |
x *= 5 |
x = x * 5 |
**= |
x **= 5 |
x = x ** 5 |
String Concatenation¶
The + operator can also be used to add (concatenate) strings.
Comparison Operators (Important!)¶
In JavaScript, there is a big difference between == and ===.
- Loose Equality (
==): Compares values after converting them to a common type (Type Coercion).5 == "5"returnstrue. - Strict Equality (
===): Compares both value and type.5 === "5"returnsfalse(because Number is not String).
Always use Strict Equality
To avoid bugs caused by JavaScript's automatic type conversion, professional developers always use === and !== instead of == and !=.