Operators in JavaScript
Operators in JavaScript are symbols or keywords used to perform operations on values and variables. They allow us to perform calculations, compare values, assign values, combine conditions, and perform other operations in a program.
For example:
let a = 10;
let b = 5;
console.log(a + b);
Output:
15
Here, + is an operator used to add two values.
JavaScript provides different types of operators, such as Arithmetic, Assignment, Comparison, Logical, Increment and Decrement, and Ternary operators.
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical operations on numbers.
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
Addition | 10 + 5 |
15 |
- |
Subtraction | 10 - 5 |
5 |
* |
Multiplication | 10 * 5 |
50 |
/ |
Division | 10 / 5 |
2 |
% |
Modulus | 10 % 3 |
1 |
** |
Exponentiation | 2 ** 3 |
8 |
Example
let a = 10;
let b = 3;
console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
console.log(a % b);
console.log(a ** b);
Output:
13
7
30
3.3333333333333335
1
1000
The % operator returns the remainder of a division, while the ** operator is used to calculate the power of a number.
2. Assignment Operators
Assignment operators are used to assign values to variables.
The basic assignment operator is =.
let age = 20;
console.log(age);
Output:
20
JavaScript also provides compound assignment operators.
| Operator | Example | Equivalent To |
|---|---|---|
= |
a = 10 |
a = 10 |
+= |
a += 5 |
a = a + 5 |
-= |
a -= 5 |
a = a - 5 |
*= |
a *= 5 |
a = a * 5 |
/= |
a /= 5 |
a = a / 5 |
%= |
a %= 5 |
a = a % 5 |
For example:
let a = 10;
a += 5;
console.log(a);
Output:
15
Here, a += 5 is the same as a = a + 5.
3. Comparison Operators
Comparison operators are used to compare two values. The result of a comparison is always a Boolean value: true or false.
| Operator | Meaning |
|---|---|
== |
Equal to |
=== |
Strictly equal to |
!= |
Not equal to |
!== |
Strictly not equal to |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal to |
<= |
Less than or equal to |
For example:
let a = 10;
let b = 5;
console.log(a > b);
console.log(a < b);
console.log(a === b);
Output:
true
false
false
== vs ===
The == operator compares values after performing type conversion when necessary, while === compares both the value and the data type without performing type conversion.
console.log(10 == "10");
console.log(10 === "10");
Output:
true
false
4. Logical Operators
Logical operators are used to combine two or more conditions. They return a Boolean result, either true or false.
JavaScript has three main logical operators:
| Operator | Name | Meaning |
|---|---|---|
&& |
Logical AND | Returns true only when both conditions are true. |
OR |
Logical OR | Returns true when at least one condition is true. |
! |
Logical NOT | Reverses the result. true becomes false, and false becomes true. |
Logical AND &&
The && operator returns true only when both conditions are true.
let age = 25;
console.log(age > 18 && age < 30);
Output:
true
Here, both conditions are true, so the result is true.
Logical OR ||
The || operator returns true when at least one condition is true.
let age = 25;
console.log(age < 18 || age > 20);
Output:
true
The second condition is true, so the overall result is true.
Logical NOT !
The ! operator reverses a Boolean value.
let isStudent = true;
console.log(!isStudent);
Output:
false
Because the original value is true, the ! operator changes it to false.
5. Increment and Decrement Operators
The increment operator ++ increases a value by 1, while the decrement operator -- decreases a value by 1.
Increment Operator
let count = 5;
count++;
console.log(count);
Output:
6
Decrement Operator
let count = 5;
count--;
console.log(count);
Output:
4
Both operators can be used in prefix and postfix forms.
let a = 5;
console.log(++a);
Output:
6
6. Ternary Operator
The ternary operator is a short way of writing a simple if...else condition. It uses three parts: a condition, a value when the condition is true, and a value when the condition is false.
The syntax is:
condition ? valueIfTrue : valueIfFalse;
For example:
let age = 20;
let result = age >= 18 ? "Adult" : "Minor";
console.log(result);
Output:
Adult
Here, the condition age >= 18 is true, so "Adult" is assigned to result.
7. String Operator
The + operator can also be used to combine strings. This process is called string concatenation.
let firstName = "John";
let lastName = "David";
let fullName = firstName + " " + lastName;
console.log(fullName);
Output:
John David
Here, the + operator combines the strings together.
Top comments (0)