Back to Javascript
Basic operators
In JavaScript, operators are symbols that allow you to perform operations on values and variables.
1. Arithmetic Operators
These operators are used to perform mathematical operations:
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | 5 + 2 | 7 |
- | Subtraction | 5 - 2 | 3 |
* | Multiplication | 5 * 2 | 10 |
/ | Division | 5 / 2 | 2.5 |
% | Modulus (Remainder) | 5 % 2 | 1 |
** | Exponentiation (ES6) | 5 ** 2 | 25 |
++ | Increment (Add 1) | let x = 5; x++ | 6 |
-- | Decrement (Subtract 1) | let x = 5; x-- | 4 |
2. Assignment Operators
These operators assign values to variables:
Operator | Example | Equivalent to |
---|---|---|
= | x = 5 | x = 5 |
+= | x += 5 | x = x + 5 |
-= | x -= 5 | x = x - 5 |
*= | x *= 5 | x = x * 5 |
/= | x /= 5 | x = x / 5 |
%= | x %= 5 | x = x % 5 |
**= | x **= 2 | x = x ** 2 |
3. Comparison Operators
These operators compare two values and return a boolean (true
or false
):
Operator | Description | Example | Result |
---|---|---|---|
== | Equal to | 5 == '5' | true |
=== | Strict equal to (type check) | 5 === '5' | false |
!= | Not equal to | 5 != '5' | false |
!== | Strict not equal (type check) | 5 !== '5' | true |
> | Greater than | 5 > 2 | true |
< | Less than | 5 < 2 | false |
>= | Greater than or equal to | 5 >= 5 | true |
<= | Less than or equal to | 5 <= 2 | false |
4. Logical Operators
These operators are used to perform logical operations:
Operator | Description | Example | Result |
---|---|---|---|
&& | Logical AND | true && false | false |
` | ` | Logical OR | |
! | Logical NOT | !true | false |
These are the fundamental operators you’ll frequently encounter when writing JavaScript code. They allow you to manipulate and compare data efficiently.