JavaScript Operators are special symbols used to perform operations on operands (values and variables). JavaScript operators include operators as in other languages. For example, 1 * 2, where * sign is an operator and 1 is left operand and 2 is a right operand. + operator adds two numeric values and produces a result which is 3 in this case.
Here *
is a JavaScript operator that performs multiplication, and 2
and 3
are operands.
Operators and Operands
The numbers (in an arithmetic operation) are called operands.
The operation (to be performed between the two operands) is defined by an operator.
The multiplication operator (*) multiply numbers:
JavaScript Operators – types
- Assignment Operators
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- String Operators
- Other Operators
JavaScript Assignment Operators
Assignment operators are used for assigning values to variables. For example,
Here, the =
the operator is used to assign value 5
to variable x
.
Here’s a list of commonly used assignment operators:
Operator | Name | Example |
---|---|---|
= | Assignment operator | a = 7; // 7 |
+= | Addition assignment | a += 5; // a = a + 5 |
-= | Subtraction Assignment | a -= 2; // a = a - 2 |
*= | Multiplication Assignment | a *= 3; // a = a * 3 |
/= | Division Assignment | a /= 2; // a = a / 2 |
%= | Remainder Assignment | a %= 2; // a = a % 2 |
**= | Exponentiation Assignment | a **= 2; // a = a**2 |
Note: The commonly used assignment operator is =
. You will understand other assignment operators such as +=
, -=
, *=
etc. once we learn arithmetic operators.
<!DOCTYPE html> <html> <body> <p id="tacode"></p> <script> var y = 15; document.getElementById("tacode").innerHTML = y; </script> </body> </html>
Check its output:
JavaScript Arithmetic Operators
Arithmetic JavaScript operators are used to perform arithmetic calculations. For example:
Here, the +
operator is used to add two operands.
Operator | Name | Example |
---|---|---|
+ | Addition | x + y |
- | Subtraction | x - y |
* | Multiplication | x * y |
/ | Division | x / y |
% | Remainder | x % y |
++ | Increment (increments by 1) | ++x or x++ |
-- | Decrement (decrements by 1) | --x or x-- |
** | Exponentiation (Power) | x ** y |
<!DOCTYPE html> <html> <body> <p id="tacode"></p> <script> var y = 150 + 50; document.getElementById("tacode").innerHTML = y; </script> </body> </html>
Output
JavaScript Logical Operators
Logical operators perform logical operations and return a boolean value, either true
or false
.
Operator | Description | Example |
---|---|---|
&& | Logical AND: true if both the operands are true , else returns false | x && y |
|| | Logical OR: true if either of the operands is true ; returns false if both are false | x || y |
! | Logical NOT: true if the operand is false and vice-versa. | !x |
JavaScript Comparison Operators
JavaScript language includes operators that compare two operands and return the Boolean values true or false.
Operators | Description |
---|---|
== | Compares the equality of two operands without considering type. |
=== | Compares equality of two operands with type. |
!= | Compares inequality of two operands. |
> | Checks whether the left-side value is greater than the right-side value. If yes then returns true otherwise false. |
< | Checks whether the left operand is less than the right operand. If yes then returns true otherwise false. |
>= | Checks whether the left operand is greater than or equal to the right operand. If yes then returns true otherwise false. |
<= | Checks whether the left operand is less than or equal to the right operand. If yes then returns true otherwise false. |
JavaScript Ternary Operator
JavaScript includes a special operator called ternary operator : ? that assigns a value to a variable based on some condition. This is like a short form of the if-else condition.
Syntax:
The ternary operator starts with conditional expression followed by ? operator. The second part ( after ? and before : operator) will be executed if the condition turns out to be true. If the condition becomes false then the third part (after 🙂 will be executed.
JS Bitwise Operators
Bitwise operators perform operations on binary representations of numbers.
Operator | Description |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | Left shift |
>> | Sign-propagating right shift |
>>> | Zero-fill right shift |
Bitwise operators are rarely used in everyday programming.