Operator in JavaScript
Operators are symbols that perform operations on operands. JavaScript supports various types of operators, including arithmetic, assignment, comparison, logical, and more. Let's explore some common operators used in JavaScript:
1. Arithmetic Operators:​
Arithmetic operators are used to perform mathematical calculations.
Addition (+
):​
The addition operator is used to add two operands.
let sum = 10 + 5; // sum = 15
Subtraction (-
):​
The subtraction operator is used to subtract the second operand from the first.
let difference = 20 - 8; // difference = 12
Multiplication (*
):​
The multiplication operator is used to multiply two operands.
let product = 5 * 4; // product = 20
Division (/
):​
The division operator is used to divide the first operand by the second.
let quotient = 50 / 10; // quotient = 5
Modulus (%
):​
The modulus operator returns the remainder of the division operation.
let remainder = 15 % 4; // remainder = 3
2. Assignment Operators:​
Assignment operators are used to assign values to variables.
Assignment (=
):​
The assignment operator assigns the value on the right to the variable on the left.
let x = 10; // x = 10
Addition Assignment (+=
):​
The addition assignment operator adds the value on the right to the variable's current value.
let y = 5;
y += 3; // y = 8
Subtraction Assignment (-=
):​
The subtraction assignment operator subtracts the value on the right from the variable's current value.
let z = 10;
z -= 2; // z = 8
3. Comparison Operators:​
Comparison operators are used to compare two values.
Equal (==
):​
The equal operator checks if two values are equal. But it does not consider the data type.
let isEqual = 5 == '5'; // isEqual = true
Strict Equal (===
):​
The strict equal operator checks if two values are equal and of the same data type.
let isStrictEqual = 5 === '5'; // isStrictEqual = false
Not Equal (!=
):​
The not equal operator checks if two values are not equal.
let isNotEqual = 10 != 5; // isNotEqual = true
Greater Than (>
):​
The greater than operator checks if the left operand is greater than the right operand.
let isGreaterThan = 15 > 10; // isGreaterThan = true
Less Than (<
):​
The less than operator checks if the left operand is less than the right operand.
let isLessThan = 5 < 10; // isLessThan = true
4. Logical Operators:​
Logical operators are used to combine multiple conditions.
Logical AND (&&
):​
The logical AND operator returns true
if both conditions are true
.
let condition1 = true;
let condition2 = false;
let result = condition1 && condition2; // result = false
Logical OR (||
):​
The logical OR operator returns true
if at least one condition is true
.
let condition1 = true;
let condition2 = false;
let result = condition1 || condition2; // result = true
Logical NOT (!
):​
The logical NOT operator returns the opposite of the condition.
let condition = true;
let result = !condition; // result = false
5. Ternary Operator:​
The ternary operator is a shorthand for an if...else
statement.
let age = 20;
let message = age >= 18 ? 'You can vote' : 'You are too young to vote';