JavaScript Operators
What operators are?
A JavaScript operator is a symbol or keyword that performs operations on values, variables, or expressions, such as adding numbers, comparing values, assigning data, or checking conditions.
Operators
▻ Arithmetic : - Used for basic math calculations.
+ adds values
let bill = 100;
let gst = 18;
let totalBill = bill + gst;
console.log(totalBill); // 118
- subtracts values
let bill = 500;
let discount = 50;
let finalBill = bill - discount;
console.log(finalBill); // 450
∗ multiplies values
let price = 100;
let quantity = 3;
let total = price * quantity;
console.log(total); // 300
/ divides values
let maxMarks = 500;
let marksGot = 460;
let percentage = (marksGot / maxMarks ) * 100;
console.log(percentage); //92
% gives remainder
let marks = 47;
let isOdd = marks % 2;
console.log(isOdd); // 1
▻ Comparison : - Used to compare two values.
The result is always true or false.
== Equal to
Checks if two values are equal, but it does not strictly check data type.
let userId = 10;
console.log(userId == "10"); // true
// Here, JavaScript converts "10" into 10, so the result is true.
=== Strict equal to
Checks both value and data type.
let userId = 10;
console.log(userId === "10"); // false
//Here, 10 is a number and "10" is a string, so the result is false.
!= Not equal to
Checks if two values are not equal.
let price = 100;
console.log(price != 200); // true
// Here, 100 is not equal to 200, so the result is true.
!= = Strict not equal to
Checks if value or data type is different.
let age = 18;
console.log(age !== "18"); // true
// Here, 18 is a number and "18" is a string, so the result is true.
> Greater than
Checks if the left value is greater than the right value.
let walletBalance = 1000;
let productPrice = 700;
console.log(walletBalance > productPrice); // true
// Here, 1000 is greater than 700.
< Less than
Checks if the left value is less than the right value.
let stock = 3;
let minimumStock = 5;
console.log(stock < minimumStock); // true
// Here, 3 is less than 5.
>= Greater than or equal to
Checks if the left value is greater than or equal to the right value.
let age = 18;
console.log(age >= 18); // true
// Here, age is exactly 18, so the result is true.
<= Less than or equal to
Checks if the left value is less than or equal to the right value.
let cartItems = 5;
let maxItemsAllowed = 5;
console.log(cartItems <= maxItemsAllowed); // true
// Here, 5 is equal to 5, so the result is true.
▻ Logical: - Check conditions and return true or false.
&& Logical AND
Returns true only when both conditions are true.
let age = 20;
let hasVoterId = true;
console.log(age >= 18 && hasVoterId); // true
//Here, the person is above 18 and also has a voter ID, so the result is true.
| A | B | A && B |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
|| Logical OR
Returns true when at least one condition is true.
let hasEmail = false;
let hasPhoneNumber = true;
console.log(hasEmail || hasPhoneNumber); // true
// Here, email is not available, but phone number is available, so the result is true.
| A | B | A || B | | --- | --- | --- | | true | true | true | | true | false | true | | false | true | true | | false | false | false |
! Logical NOT
Reverses the result.
let isLoggedIn = false;
console.log(!isLoggedIn); // true
// Here, isLoggedIn is false, but ! changes it to true.
| A | !A |
|---|---|
| true | false |
| false | true |
▻ Assignment: - Assign or update values in variables.
= Assign value
Stores a value inside a variable.
let name = "Rahul";
console.log(name); // Rahul
// Here, "Rahul" is assigned to the variable name.
+= Add and assign
Adds a value to the existing variable value.
let walletBalance = 1000;
walletBalance += 500;
console.log(walletBalance); // 1500
// Here, 500 is added to walletBalance.
-= Subtract and assign
Subtracts a value from the existing variable value.
let walletBalance = 1000;
walletBalance -= 300;
console.log(walletBalance); // 700
// Here, 300 is subtracted from walletBalance.
Summary
| Arithmetic operators | + - ∗ / % |
|---|---|
| Comparison operators | == === != !== > < |
| Logical operators | && |
| Assignment operators | = += -= |
