Hello readers ๐, welcome to the second blog of this JavaScript series.
In this article, weโll explore JavaScript Operators - the powerful tools that allow us to perform calculations, compare values, and make decisions in our programs.
In real life, you perform some operations like:-
- Add two numbers (Mathematical operations)
- Compare prices (Comparison)
- Make decisions
In JavaScript, these actions can be performed using operators.
Definition
An operator is a symbol or keyword in programming that tells the computer to perform mathematical, logical, or comparison operations on data (operands) to produce a result.
Types of operators in JavaScript:
- Assignment Operator
- Arithmetic Operator
- Comparison Operator
- Logical Operator
Assignment Operator
It is an operator that is used to assign values to a variable. The most commonly used assignment operator is =(Equal to) operator.
Usage:
let x = 10; // The value 10 is stored in the variable x.
Other Assignment Operators:
Given that x = 10 and y = 5, the table below explains the assignment operators:
| Operator | Example | Same As | Result |
|---|---|---|---|
| = | x = y | x = y | x = 5 |
| += | x += y | x = x + y | x = 15 |
| -= | x -= y | x = x - y | x = 5 |
| *= | x *= y | x = x * y | x = 50 |
| **= | x **= y | x = x ** y | x = 100000 |
| /= | x /= y | x = x / y | x = 2 |
| %= | x %= y | x = x % y | x = 0 |
Arithmetic Operator
This operator is used to perform Mathematical operations on the values(Operands).
Different types of Arithmetic operator are:
1. Addition
+ is used to add two values. In case of numbers it does simple addition, but in case of strings, it concatenates(Joins them) the strings.
Example:
let x = 10;
let y = 5
let z = "2"
let fname = "satya"
let lname = "sootar"
console.log(x + y) // 15
console.log(x + z) // 102
console.log(fname + lname) //satyasootar
console.log(fname + x) //satya10
2. Subtraction
- is used to subtract two values. In case of number, it does simple subtraction, but for strings it returns NaN - Not A Number. Two strings cannot be subtracted. But if one value is a string that contains a number (like "2") and the other is a number, JavaScript converts the string to a number before performing the operation.
Example:
let x = 10;
let y = 5
let z = "2"
let fname = "satya"
let lname = "sootar"
console.log(x - y) // 5
console.log(x - z) // 8
console.log(fname - lname) // NaN
console.log(fname - x) //NaN
3. Multiplication
* is used to perform Multiplication between two numbers.
Example:
let x = 10;
let y = 5
console.log(x * y) // 50
4. Division
/ is used to perform Division between two numbers. It divides the left operand by the right operand.
Example:
let x = 9;
let y = 5
let z = "3"
let fname = "satya"
let lname = "sootar"
console.log(x / y) // 1.8
console.log(x / z) // 3
console.log(fname / lname) // NaN
console.log(fname / x) //NaN
5. Remainder
% - Also known as the modulo operator, it returns the remainder of an integer division.
let x = 9;
let y = 5
console.log(x % y) // 4
6. Exponentiation
** Raises the first operand to the power of the second operand (equivalent to Math.pow()).
Example:
let x = 9;
let y = 5;
console.log(x ** y) // 59049
7. Increment
Increases the value of a variable by one. It can be used in prefix form (++x, increments then returns the value) or postfix form (x++, returns the value then increments).
let x = 9;
let y = 5
console.log(x++) // 9 - Returns the value then it will increase.
console.log(x) // 10 - Value of x increased.
console.log(++y) // 6 - First increase the value then returns it.
8. Decrement
Decreases the value of a variable by one. It can be used in prefix form (--x, decrement then returns the value) or postfix form (x--, returns the value then decrement).
let x = 9;
let y = 5
console.log(x--) // 9 - Returns the value then it will decrease.
console.log(x) // 8 - Value of the x decreased.
console.log(--y) // 4 - First decrease the value then returns it.
Comparison Operator
Comparison operators are used to compare two values.
The result of a comparison is always a Boolean value - either true or false.
Different types of Comparison Operators are:
| Operator | Example | Description |
|---|---|---|
| == | x == y | Checks if values are equal (ignores datatype) |
| === | x === y | Checks if values and datatype are equal |
| != | x != y | Checks if values are not equal |
| !== | x !== y | Checks if values or datatype are not equal |
| > | x > y | Checks if left value is greater |
| < | x < y | Checks if left value is smaller |
| >= | x >= y | Checks if left value is greater or equal |
| <= | x <= y | Checks if left value is smaller or equal |
Example:
let x = 10;
let y = 5;
let z = "10";
console.log(x == z) // true (Only values are compared)
console.log(x === z) // false (Datatype is also compared)
console.log(x != y) // true
console.log(x > y) // true
console.log(x < y) // false
console.log(x >= 10) // true
console.log(y <= 3) // false
Important Difference: == vs ===
== โ Loose Equality (only values are compared)
=== โ Strict Equality (values and datatype both compared)
Example:
console.log(5 == "5") // true
console.log(5 === "5") // false
In real-world development, developers usually prefer === because it avoids unexpected type conversion bugs.
Logical Operators
Logical operators are used to combine multiple conditions.
They are mostly used in decision making and control statements like if, while, etc.
Types of Logical Operators
| Operator | Name | Description |
|---|---|---|
&& |
Logical AND | Returns true if both conditions are true |
|| |
Logical OR | Returns true if at least one condition is true |
! |
Logical NOT | Reverses the result |
1. Logical AND (&&)
Returns true only if both conditions are true.
Truth Table
| A | B | A && B |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
Example
let age = 20;
let hasID = true;
console.log(age > 18 && hasID); // true
Real Life Example
You can enter a club only if:
- your age > 18
- you have an ID proof
Both conditions must be true.
2. Logical OR (||)
Returns true if at least one condition is true.
Truth Table
| A | B | A || B |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
Example
let marks = 35;
let sportsQuota = true;
console.log(marks > 40 || sportsQuota); // true
Real Life Example
You can get college admission if:
- you have good marks
- OR
- you have sports quota
Only one condition needs to be true.
3. Logical NOT (!)
Logical NOT reverses the result.
true โ false
false โ true
Truth Table
| A | !A |
|---|---|
| true | false |
| false | true |
Example
let isLoggedIn = false;
console.log(!isLoggedIn); // true
Real Life Example
If a user is not logged in, show the login page.
Conclusion
Operators are one of the fundamental building blocks of JavaScript. They allow us to:
- Perform mathematical calculations
- Compare values
- Make decisions
- Assign data to variables
Without operators, writing meaningful programs would be almost impossible.
As you move forward in JavaScript, you'll use these operators constantly in:
- Conditions (
if,else) - Loops
- Functions
- Real-world applications
Hope you liked this blog. If thereโs any mistake or something I can improve, do tell me. You can find me on LinkedIn and X, I post more stuff there.
Top comments (0)