JavaScript Operators
JavaScript Operators
What is an operator?
Operators are used to assign values, compare values, perform arithmetic operations, and more.
Let us take a simple expression 4 + 5 is equal to 9. Here 4 and 5 are called operands and ‘+’ is called
the operator. JavaScript supports the following types of operators.
Arithmetic Operators
Comparison Operators
Logical or Relational Operators
Assignment Operators
Arithmetic Operators:
In JavaScript, arithmetic operators are symbols or functions that allow you to perform basic
mathematical operations on numeric values.
1 + Addition
2 – Subtraction
3 * Multiplication
4. / Division
5 % Modulus
7 -- Decrement
Note − Addition operator + works for Numeric as well as Strings. e.g. "a" + 10
will give "a10".
Comparison Operators:
Comparison operators in JavaScript are used to compare values and determine the relationship
between them. They return a Boolean value (true or false) based on whether the comparison is true
or false.
1 = = Equal
Checks if the value of two operands are equal or not, if yes, then the condition becomes true.
2 != Not Equal
Checks if the value of two operands are equal or not, if the values are not equal, then the condition
becomes true.
Ex: A! = B is true.
Checks if the value of the left operand is greater than the value of the right operand, if yes, then the
condition becomes true.
Checks if the value of the left operand is less than the value of the right operand, if yes, then the
condition becomes true.
5 >= GreaterthanorEqualto
Checks if the value of the left operand is greater than or equal to the value of the right operand, if
yes, then the condition becomes true.
6 <= LessthanorEqualto
Checks if the value of the left operand is less than or equal to the value of the right operand, if yes,
then the condition becomes true.
Logical Operators:
1 && LogicalAND
If both the operands are true, then the condition becomes true.
2 || LogicalOR
If any of the two operands are non-zero, then the condition becomes true.
Ex: A | | B is true.
False , false
3 ! LogicalNOT
Reverses the logical state of its operand. If a condition is true, then the Logical NOT operator will
make it false.
Assignment Operators:
1 = SimpleAssignment
Assigns values from the right side operand to the left side operand
2 += AddandAssignment
It adds the right operand to the left operand and assigns the result to the left operand.
Ex: C += A is equivalent to C = C + A
3 −= SubtractandAssignment
It subtracts the right operand from the left operand and assigns the result to the left operand.
Ex: C -= A is equivalent to C = C – A
4 *= MultiplyandAssignment
It multiplies the right operand with the left operand and assigns the result to the left operand.
Ex: C *= A is equivalent to C = C * A
5 /= DivideandAssignment
It divides the left operand with the right operand and assigns the result to the left operand.
Ex: C /= A is equivalent to C = C / A
6 %= ModulesandAssignment
It takes modulus using two operands and assigns the result to the left operand.
Ex: C %= A is equivalent to C = C % A