An operator is capable of manipulating a certain value or operand. Operators are used to performing specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands. In JavaScript, operators are used for comparing values, performing arithmetic operations, etc.
There are various operators supported by JavaScript, like Arithmetic, Assignment, Bitwise, Comparison, Logical, Ternary, and typeof and there are lots of other operators that you can learn.
Arithmetic Operators: These operators work on numerical values and then return a number. These are basically used to perform mathematical operations but we can also use them on strings to concatenate the strings.
JavaScript Arithmetic Operators: This table contains the operators’ names, operations, and a pseudo-code of example.
OPERATOR NAME |
OPERATION |
EXAMPLE |
---|---|---|
Addition(+) | Add two numbers or concatenate the string. | Y = 5 + 5 gives Y = 10 Y = “Geeks” + “for” + “Geeks” gives Y = “neveropen” Y = “Geeks” + 4 + “Geeks” gives Y = “Geeks4Geeks” |
Subtraction(-) | Difference between the two operators. | Y = 5 – 3 gives Y = 2 |
Multiplication(*) | Multiply two or more numbers. | Y = 5 * 5 gives Y = 25 |
Division(/) | Find the quotient of two operands. | Y = 5 / 5 gives Y = 1 |
Modulus(%) | Find the remainder of two operands. | A % B means remainder (A/B) Y = 5 % 4 gives Y = 1 |
Exponentiation(**) | Raise the Left operator to the power of the right operator. | Y = 5 ** 3 gives Y = 125 |
Increment(++) | Increase the operand by one. | let A = 10 and Y = A + + then A = 11, Y=10 if A = 10 and Y = + + A then A = 11, Y=11 |
Decrement(–) | Decrease the operand by one. | let A = 10 and Y = A – – then A = 9, Y=10 if A = 10 and Y = – – A then A = 9, Y=9 |
Unary Plus(+) | Converts non-numbers to numbers. | let X = “10” and Y = +X then typeof(X) will be string and typeof(y) will be positive number |
Unary Negation (-) | Converts operand to negative. | let X = “10” and Y = -X then typeof(X) will be string and typeof(y) will be negative number |
JavaScript Comparison Operators: This table contains the operators’ names, operations, and a pseudo-code of example.
Comparison Operators: These comparison operators are mainly used to perform the logical operations that determine the equality or difference between the values.
OPERATOR NAME |
OPERATION |
EXAMPLE |
---|---|---|
Equality Operator(==) | Compares the equality of two opearands. | Y = 5 and X = 6 Y = = X is false |
Strict equality (===) | Compares both value and type of the operands. | Y = 5 and X = ‘5’ Y = = = X is false |
Inequalityl (!=) | Compares inequality of two operands. | let X = 10 then X ! = 11 is true |
Strict inequality(!==) | Compares inequality of two operands. | let X = 10 then X ! == 11 is true |
Greater than (>) | Checks if the left operator is greater than the right operand. | let X = 10 then X > 11 is false |
Less than (<)< /strong> | Checks if the left operator is smaller than the right operand. | let X = 10 then X < 11 is true |
Greater than or Equal to (> =) | Checks if the left operator is greater than or equal to the right operand. | let X = 10 then X > = 11 is false |
Less than or Equal to (<= ) | Checks if the left operator is smaller than or equal to the right operand. | let X = 10 then X <=10 is true. |
Bitwise Operators: These operators uses 32 bits BItwise operands. A number is stored as a 64-bit floating-point number but the bit-wise operation is performed on a 32-bit binary number.
JavaScript Bitwise Operators: This table contains the operators’ names, operations, and a pseudo-code of example.
OPERATOR NAME |
OPERATION |
EXAMPLE |
---|---|---|
Bitwise AND(&) | Returns true if both operands are true | |
Bitwise OR(|) | Returns true even if one operand is true | |
Biwise XOR(^) | Returns true if both operands are different | |
Bitwise NOT(~) | Flips the value of the operand. | |
Bitwise Left Shift(<<) | Shifts the bit toward left. | |
Bitwise Right Shift(>>) | Shifts the bit towards the right. | |
Zero Fill Right Shift(>>>) | Shifts the bit towards right but adds 0 from left. |
Logical Operators: These logical operator is mostly used to make decisions based on conditions specified for the statements. It can also be used to manipulate a boolean or set termination conditions for loops.
JavaScript Logical Operators: This table contains the operators’ names, operations, and a pseudo-code of example.
OPERATOR NAME |
OPERATION |
EXAMPLE |
---|---|---|
NOT(!) | Converts operands to boolean and returns flipped value. | let i=0 and console.log((!1)) is true |
AND(&&) | Evaluates operands and return true only if all are true. | let i = 0, j=2, k=3; then i&&j&&k = fasle |
OR(||) | Returns true even if one of the multiple operands is true. | let i = 0, l=0; then i||l = true |
Assignment Operators: These assignment operator is equal (=) which assigns the value of the right-hand operand to its left-hand operand. That is if a = b assigns the value of b to a.
JavaScript Assignment Operators: This table contains the operators’ names, operations, and a pseudo-code of example.
OPERATOR NAME |
OPERATION |
EXAMPLE |
---|---|---|
Addition Assignment(+=) | Sums up left and right operand values and then assign the result to the left operand. | Y += 1 gives Y = Y + 1 |
Subtraction Assignment(-=) | Subtract right side value from left side value and then assign the result to the left operand. | Y -= 1 gives Y = Y – 1 |
Multiplication Assignment(*=) | Multiplies a variable by the value of the right operand and assigns the result to the variable. | Y *= A is equivalent to Y = Y * A |
Division Assignment(/=) | Divides a variable by the value of the right operand and assigns the result to the variable. | Y /= A is equivalent to Y = Y / A |
Modulus Assignment(%=) | Divides a variable by the value of the right operand and assigns the remainder to the variable. | Y %= A is equivalent to Y = Y % A |
Exponentiation Assignment (** =) | Raise the Left operator to the power of the right operator. | Y = 5 ** 3 gives Y = 125 |
Left Shift Assignment (<<=) | Moves the specified amount of bits to the left and assigns the result to the variable. | Y <<= A is equivalent to Y=Y << A |
Right Shift Assignment (>> =) | Moves the specified amount of bits to the right and assigns the result to the variable. | Y >>= A is equivalent to Y = Y >> A |
Bitwise AND Assignment (& =) | Does a bitwise AND operation on the operand, and assigns the result to the variable. | Y &= b is equivalent to Y = Y & A |
Bitwise OR Assignment (| =) | Does a bitwise OR operation on the operand, and assigns the result to the variable. | Y |= A is equivalent to Y= Y | b |
Bitwise XOR Assignment (^ =) | Does a bitwise XOR operation on the operand, and assigns the result to the variable. | Y ^= A is equivalent to Y= Y ^ A |
JavaScript typeof Operator: In JavaScript, the typeof operator returns the data type of its operand in the form of a string. The operand can be any object, function, or variable.
Example:
javascript
var a = 17; var b = "neveropen" ; var c = "" ; var d = null ; console.log( "Type of a = " + ( typeof a)); console.log( "Type of b = " + ( typeof b)); console.log( "Type of c = " + ( typeof c)); console.log( "Type of d = " + ( typeof d)); console.log( "Type of e = " + ( typeof e)); |
Output:
Type of a = number Type of b = string Type of c = string Type of d = object Type of e = undefined
We have a complete list of Javascript Operators, to check those please go through this Javascript Operators Complete Reference article.