Operators in JavaScript
The different type of operators in JavaScript. Operators are symbols used to perform different operations.
* Addition Operator
* Assignment Operator
* Addition Assignment Operator
* Subtraction Operator
* Subtraction Assignment Operator
* Multiplication Operator
* Division Operator
1. Addition Operator (+)
The + operator is used to add two numbers.
let a = 10;
let b = 5;
console.log(a + b);
Output:
15
2. Assignment Operator (=)
The = operator is used to assign a value to a variable.
let number = 10;
console.log(number);
Output:
10
Here, 10 is assigned to number.
3. Addition Assignment Operator (+=)
The += operator adds a value to the existing value.
let number = 10;
number += 5;
console.log(number);
Output:
15
It is the same as:
number = number + 5;
In my calculator, += is also used to add a new digit to the existing value.
currentValue += value;
If currentValue is "10" and value is "5", the output becomes "105" because they are strings.
4. Subtraction Operator (-)
The - operator is used to subtract one number from another.
let a = 10;
let b = 5;
console.log(a - b);
Output:
5
5. Subtraction Assignment Operator (-=)
The -= operator subtracts a value from the existing value.
let number = 10;
number -= 5;
console.log(number);
Output:
5
It is the same as:
number = number - 5;
6. Multiplication Operator (*)
The * operator is used to multiply two numbers.
let a = 10;
let b = 5;
console.log(a * b);
Output:
50
7. Division Operator (/)
The / operator is used to divide one number by another.
let a = 10;
let b = 5;
console.log(a / b);
Output:
2
*Comparison Operators
Comparison operators are used to compare two values.
They return either true or false.
-
means more than and < means less than.
- == checks whether two values are equal.
- === checks both value and data type.
Example:
let a = 10;
let b = 5;
console.log(a > b); // true
console.log(a < b); // false
console.log(a == 10); // true
console.log(a === 10); // true
output:
true
false
true
true
Logical Operators
Logical operators are used to combine two or more conditions.
&& means AND and checks if all conditions are true.
|| means OR and checks if at least one condition is true.
! means NOT and changes true to false or false to true.
Logical operators are commonly used with if conditions.
Example:
&& AND Operator
The && operator checks whether both conditions are true.
Example:
let age = 20;
console.log(age > 18 && age < 30);
Output:
true
- OR Operator
The || operator checks whether at least one condition is true.
Example:
let age = 20;
console.log(age > 18 || age < 10);
Output:
true
- ! NOT Operator
The ! operator changes true to false and false to true.
Example :
let age = 20;
console.log(!(age > 18));
Output:
false
Increment and Decrement Operators
The ++ operator increases a value by 1.
The -- operator decreases a value by 1.
These operators are used to increase or decrease a variable.
They are commonly used in loops and calculations.
The value changes directly when the operator is used.
Example :
let number = 10;
number++;
console.log(number);
number--;
console.log(number);
Output:
11
10
Modulus Operator (%)
- The % operator is used to find the remainder after division.
- It gives the remaining value after one number is divided by another.
Example:
let a = 10;
let b = 3;
console.log(a % b);
Output:
1
Exponentiation Operator (**)
The ** operator is used to calculate the power of a number.
It raises the first number to the power of the second number.
For example, 2 ** 3 means 2 × 2 × 2.
The result is 8.
It is also called the power operator.
Example :
let a = 2;
let b = 3;
console.log(a ** b);
output:
8
Not Equal Operator (!=)
The != operator checks whether two values are not equal.
If the values are different, it returns true.
If the values are the same, it returns false.
It checks the value but does not strictly check the data type.
It is used to compare two different values.
Example:
let a = 10;
let b = 5;
console.log(a != b);
output :
true
Strict Not Equal Operator (!==)
-
The !== operator checks whether two values are not equal.
- It checks both the value and data type.
-
If either the value or data type is different, it returns true.
- If both are the same, it returns false.
It is called the Strict Not Equal Operator.
Example:
let a = 10;
let b = "10";
console.log(a !== b);
Output:
true
10 is a number and "10" is a string.
Top comments (0)