By using the examples in this tutorial, you will learn about the different operators available in JavaScript and how to use them.
Operators
JavaScript operators are symbols that are used to perform operations on operands(values and variables). For example,
1 + 2 ;
Here, +
is an operator and 1
and 2
are operands.
Different types of operators available in JavaScript are listed below.
- Assignment Operators
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- String Operators
Assignment Operators
An assignment operator
assigns a value to its left operand according to the value of its right operand. For example,
const x = 1;
Here, =
operator is used to assigning value 1 to variable x
.
The different assignment operators available are shown below.
Operator | Name | Example |
= | Assignment operator | x = 10 //10 |
+= | Addition assignment | x += 10; // x = x +10 |
-= | Subtraction Assignment | x -= 10; // x = x -10 |
*= | Multiplication Assignment | x *= 10; // x = x *10 |
/= | Division Assignment | x /= 10; // x = x /10 |
%= | Remainder Assignment | x %= 10; // x = x %10 |
Arithmetic Operators
Using arithmetic operators
, one can perform arithmetic operations on operands. For example,
const opr = 1 + 6; // 7
Here, the +
operator is used to add two operands.
The different arithmetic operators available are shown below.
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 |
Comparison Operators
Comparison operators
compare operands and return a logical value based on whether the comparison is true. Operands can be numerical, string, logical, or object values. Comparison operators are used in decision-making and loops. For example,
const a = 3, b = 2, c = 3;
console.log(a > b); // true
console.log(a == c); // true
console.log(a === c); // false
Here, the comparison operator >
is used to compare whether a is greater than b and a
is equal to c
.
The different comparison operators available are shown below.
Operator | Description | Example |
== | Equal to: returns true if the operands are equal | x == y |
!= | Not equal to returns true if the operands are not equal | x != y |
=== | Strict equal to: true if the operands are equal and of the same type | x === y |
!== | Strict not equal to: true if the operands are equal but of different types or not equal at all | x !== y |
> | Greater than: true if the left operand is greater than the right operand | x > y |
>= | Greater than or equal to: true if the left operand is greater than or equal to the right operand | x >= y |
< | Less than: true if the left operand is less than the right operand | x < y |
<= | Less than or equal to: true if the left operand is less than or equal to the right operand | x <= y |
Logical Operators
Logic operators
perform logical operations and return a boolean value, which is either true or false. For example,
const x = 5, y = 3;
(x < 6) && (y < 5); // true
Here, &&
is the logical operator AND. Since both x < 6
and y < 5
are true
, the result is true
.
The different logical operators available are shown below.
Operators | 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 |
Bitwise Operators
Bitwise operators treat their operands as sets of 32 bits (zeroes and ones), rather than decimal, hexadecimal, or octal numbers. For example, the decimal number nine has a binary representation of 1001. Bitwise operators perform their operations on such binary representations, but they return standard JavaScript numerical values.
The different bitwise operators available are shown below.
Operator | Description | Example |
& | Returns a one in each bit position for which the corresponding bits of both operands are ones. | a & b |
| | Returns a zero in each bit position for which the corresponding bits of both operands are zeros. | a | b |
^ | Returns a zero in each bit position for which the corresponding bits are the same and returns a one in each bit position for which the corresponding bits are different.] | a ^ b |
~ | Inverts the bits of its operand. | ~ a |
<< | Shifts a in binary representation b bits to the left, shifting in zeros from the right. | a << b |
>> | Shifts a in binary representation b bits to the right, discarding bits shifted off. | a >> b |
>>> | Shifts a in binary representation b bits to the right, discarding bits shifted off, and shifting in zeros from the left. | a >>> b |
String Operator
In JavaScript, you can use the +
operator as string operator to concatenate (join) two or more strings.
For example,
// concatenation operator
console.log('hello' + 'world');
let a = 'Hello';
a += ' World'; // a = a + ' World';
console.log(a); // Hello World
Here, +
operator performs concatenation as the operands are of string type but the same +
operator will perform an addition operation if the operands used are of integer type.
Conclusion
In this lesson, you have learned how about JavaScript operators and how to use them.