By the end of this lesson, learners will:
== and ===In JavaScript, an operator is a symbol that performs an operation on one or more values (called operands). It tells the computer to do something, like add numbers, compare values, or assign variables.
π Example:
let total = 5 + 3; // "+" is an operator
Used to perform basic mathematical calculations.
| Operator | Symbol | Example | Result |
|---|---|---|---|
| Addition | + | 5 + 2 | 7 |
| Subtraction | - | 10 - 4 | 6 |
| Multiplication | * | 3 * 3 | 9 |
| Division | / | 8 / 2 | 4 |
| Modulus (remainder) | % | 10 % 3 | 1 |
| Exponentiation | ** | 2 ** 3 | 8 |
π Example:
let a = 10;
let b = 3;
console.log(a % b); // 1 (remainder)
console.log(2 ** 4); // 16 (2 raised to the 4th power)
Used to assign values to variables.
| Operator | Example | Meaning |
|---|---|---|
= | x = 5 | Assign 5 to x |
+= | x += 2 | Add 2 to x |
-= | x -= 1 | Subtract 1 from x |
*= | x *= 3 | Multiply x by 3 |
/= | x /= 2 | Divide x by 2 |
%= | x %= 4 | x = x mod 4 |
π Example:
let x = 10;
x += 5; // same as x = x + 5
console.log(x); // 15
Used to compare values. These return Boolean results: true or false.
| Operator | Description | Example | Result |
|---|---|---|---|
== | Equal (loose) | 5 == "5" | true |
=== | Equal (strict) | 5 === "5" | false |
!= | Not equal (loose) | 5 != "5" | false |
!== | Not equal (strict) | 5 !== "5" | true |
> | Greater than | 8 > 5 | true |
< | Less than | 3 < 4 | true |
>= | Greater or equal | 10 >= 10 | true |
<= | Less or equal | 6 <= 3 | false |
== vs ===: Whatβs the Difference?== checks only value and does type coercion5 == "5" β true=== checks value and type (strict)5 === "5" β falseβ
Always use === in modern JavaScript to avoid unexpected results.
Used to combine or invert Boolean expressions.
| Operator | Name | Example | Result |
|---|---|---|---|
&& | AND | true && true | true |
| ` | ` | OR | |
! | NOT | !true | false |
π Examples:
let isLoggedIn = true;
let isAdmin = false;
console.log(isLoggedIn && isAdmin); // false
console.log(isLoggedIn || isAdmin); // true
console.log(!isLoggedIn); // false
+The + operator is also used to combine strings.
π Example:
let firstName = "Alice";
let lastName = "Smith";
let fullName = firstName + " " + lastName;
console.log(fullName); // Alice Smith
π§ If one operand is a string, + will treat the other as a string too.
JavaScript tries to convert values automatically when needed.
π Example:
console.log("5" + 1); // "51" β number is converted to string
console.log("5" - 1); // 4 β string is converted to number
β
Be careful β always use strict operators (===) to avoid confusion.
Try this in your browser console:
let a = 10;
let b = 5;
console.log(a + b); // 15
console.log(a * b); // 50
console.log(a == "10"); // true
console.log(a === "10"); // false
console.log(a > b && b < 3); // false
| Type | Operators | Use |
|---|---|---|
| Arithmetic | +, -, *, /, %, ** | Calculations |
| Assignment | =, +=, -=, etc. | Assign/change values |
| Comparison | ==, ===, !=, >, etc. | Compare values |
| Logical | &&, ` | |
| String | + | Combine (concatenate) text |