JavaScript - Operators
JavaScript - Operators
What is an Operator?
In JavaScript, anoperatoris a symbol that performs an operation on one or more operands, such as variables or values, and returns a result. Let us take a simple expression4 + 5is equal to 9. Here 4 and 5 are calledoperands, and + is called theoperator.
JavaScript supports the following types of operators.
- Arithmetic Operators
- Comparison Operators
- Logical (or Relational) Operators
- Bitwise Operators
- Assignment Operators
- Miscellaneous Operators
Lets have a look on all operators one by one.
JavaScript Arithmetic Operators
The JavaScript arithmetic operators are used to perform mathematical calculations such as addition, multiplication, subtraction, division, etc. on numbers. JavaScript supports the following arithmetic operators −
Assume variablexholds 10 and variableyholds 20, then −
OperatorDescriptionExample+ (Addition)Adds two operands.x + y will give 30.- (Subtraction)Subtracts the second operand from the first.x - y will give -10.* (Multiplication)Multiplies both operands.x * y will give 200./ (Division)Divides the numerator by the denominator.y / x will give 2.% (Modulus)Outputs the remainder of an integer division.y % x will give 0++ (Increment)Increases an integer value by one.x++ will give 11.— (Decrement)Decreases an integer value by one.x— will give 9.
Addition operator (+) works for Numeric as well as Strings. e.g. “a” + 10 will give “a10”.
JavaScript Comparison Operators
The JavaScript comparison operators compare two values and returns a boolean result (trueorfalse). JavaScript supports the following comparison operators −
Assume variablexholds 10 and variableyholds 20, then −
| Operator | Description | Example |
|---|---|---|
== | Equal | (x == y) is not true. |
!= | Not Equal | (x != y) is true. |
=== | Strict equality | (x === y) is not true. |
!== | Strict inequality | (x !== y) is true. |
> | Greater than | (x > y) is not true. |
< | Less than | (x < y) is true. |
>= | Greater than or Equal to | (x >= y) is not true. |
<= | Less than or Equal to | (x <= y) is true. |
JavaScript Logical Operators
The logical operators are generally used to perform logical operations on boolean values. But logical operators can be applied to values of any types not only boolean.
JavaScript supports the following logical operators −
Assume that the value ofxis 10 andyis 0.
OperatorDescriptionExample&& (Logical AND)If both the operands are non-zero, then the condition becomes true.(x && y) is false|| (Logical OR)If any of the two operands are non-zero, then the condition becomes true.(x || y) is true.! (Logical NOT)Reverses the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false.!x is false.
JavaScript Bitwise Operators
The JavaScript bitwise operators are used to perform bit-level operations on integers. JavaScript supports the following seven types of bitwise operators −
Assume variable x holds 2 and variable y holds 3, then −
OperatorDescriptionExample& (Bitwise AND)It performs a Boolean AND operation on each bit of its integer arguments.(x & y) is 2.| (Bitwise OR)It performs a Boolean OR operation on each bit of its integer arguments.(x | y) is 3.^ (Bitwise XOR)It performs a Boolean exclusive OR operation on each bit of its integer arguments. Exclusive OR means that either operand one is true or operand two is true, but not both.(x ^ y) is 1.~ (Bitwise Not)It is a unary operator and operates by reversing all the bits in the operand.(~y) is -4.<< (Left Shift)It moves all the bits in its first operand to the left by the number of places specified in the second operand. New bits are filled with zeros. Shifting a value left by one position is equivalent to multiplying it by 2, shifting two positions is equivalent to multiplying by 4, and so on.(x << 1) is 4.>> (Right Shift)Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.(x >> 1) is 1.>>> (Right shift with Zero)This operator is just like the >> operator, except that the bits shifted in on the left are always zero.(x >>> 1) is 1.
JavaScript Assignment Operators
In JavaScript, an assignment operator is used to assign a value to a variable. JavaScript supports the following assignment operators −
OperatorDescriptionExample= (Simple Assignment)Assigns values from the right side operand to the left side operandz = x + y will assign the value of x + y into z+= (Add and Assignment)It adds the right operand to the left operand and assigns the result to the left operand.z += x is equivalent to z = z + x= (Subtract and Assignment)It subtracts the right operand from the left operand and assigns the result to the left operand.z -= x is equivalent to z = z - x*=(Multiply and Assignment)It multiplies the right operand with the left operand and assigns the result to the left operand.z *= x is equivalent to z = z * x/= (Divide and Assignment)It divides the left operand with the right operand and assigns the result to the left operand.z /= x is equivalent to z = z / x%= (Modules and Assignment)It takes modulus using two operands and assigns the result to the left operand.z %= x is equivalent to z = z % x
Same logic applies to Bitwise operators so they will become like <<=, >>=, >>>=, &=, |= and ^=.
JavaScript Miscellaneous Operators
There are few other operators supported by JavaScript. These operators areconditionaloperator (? :),typeofoperator,deleteoperator, etc.
In the below table, we have given the JavaScript miscellaneous operators with its explanation.
OperatorDescription? : (Conditional )If Condition is true? Then value X : Otherwise value YtypeofIt returns the data type of the operand.?? (Nullish Coalescing Operator)It returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.deleteIt removes a property from an object., (Comma)It evaluates its operands (from left to right) and returns the value of the last operand.() (Grouping)It allows to change the operator precedence.yieldIt is used to pause and resume a generator function.(Spread)It is used to expand the iterables such as array or string.** (Exponentiation)Raises the left operand to the power of the right operand