In this tutorial, you will learn about different operators in javascript and how to use them with the help of multiple examples.
What is an Operator?
In javascript, operators are the special symbols that are used to perform operations on the operands(values and variables). For example,
2 + 3 // 5
Here +
is an operator that is performing an addition operation on operands 2
and 3
.
Types of Javascript Operators
Here is the list of JavaScript operators that you are going to learn in this tutorial.
Assignment Operators in Javascript
Assignment operators are used to assign values to javascript variables. For example,
let x = 5;
Here, the =
assignment operator is used to assign 5
to variable x
.
Here is the list of some commonly used assignment operators:
Operator | Name | Example |
---|---|---|
= | Assignment operator | a = 5; // 5 |
+= | Addition assignment | a += 5; // a = a + 5 |
-= | Subtraction assignment | a -= 5; // a = a - 5 |
*= | Multiplication assignment | a *= 5; // a = a * 5 |
/= | Division assignment | a /= 5; // a = a / 5 |
%= | Remainder assignment | a %= 5; // a = a % 5 |
**= | Exponentiation Assignment | a **= 2; // a = a ** 2 |
Arithmetic Operators in Javascript
Arithmetic operators are used to perform arithmetic operations. For example,
let x = 2 + 3; // 5
Here the +
operator is used to add operands 2
and 3
;
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 |
** | Exponential (Power) | x ** y (x^y) |
Example 1: Arithmetic operators in JavaScript
let x = 4;
let y = 2;
// addition
console.log(x + y); // 6
// subtraction
console.log(x - y); // 2
// multiplication
console.log(x * y); // 8
// division
console.log(x / y); // 2
// remainder
console.log(x % y); // 0
// increment
console.log(++x); // 5
console.log(x++); // prints 5 and then increments by 1
console.log(x); // 6
// decrement
console.log(--x); // 5
console.log(x--); // prints 5 and then decrements by 4
console.log(x); // 4
//exponentiation
console.log(x ** y); // 16
Comparison Operators in Javascript
Comparison operator compares two values and as a result, returns either true
or false
. For example,
let a = 5, b = 4;
console.log( a > b ); // true
Here a
and b
are the operands and we are using the >
comparison operator to compare them.
Operator | Description | Example |
---|---|---|
== | Equal to: true if operands are equal | x == y |
=== | Strict equal to: true if operands and their types are equal | x === y |
!= | Not equal to: true if operands are not equal | x != y |
!== | Strict not equal to: true if operands are not equal and nor their types | x !== y |
> | Greater than: true if the left operand is greater than the right operand | x > y |
< | Less than: true if the right operand is greater than the left operand | x < y |
>= | Greater than or equal to: true if the right operand is greater than or equal to the left operand | x >= y |
<= | less than or equal to: true if the right operand is greater than or equal to left operand | x <= y |
Example 2: Comparison operators in Javascript
// equal to
console.log(2 == 2); // true
console.log(2 == '2'); // true
// not equal to
console.log(3 != 2); // true
console.log('hello' != 'Hello'); // true
// strict equal to
console.log(2 === 2); // true
console.log(2 === '2'); // false
// strict not equal to
console.log(2 !== '2'); // true
console.log(2 !== 2); // false
Logical Operators in Javascript
Logical operators are used to perform the logical operation and return boolean values, either true or false. For example,
let x = 2, y = 3;
console.log(x < 3 && y < 4); // true
Here, &&
is AND
logical operator. It is returning true
because both x < 3
and y < 4
are true
.
Name | Description | Example |
---|---|---|
&& | Logical AND: true if both operands are true , else returns false | a && b |
|| | Logical OR: true if either of the operands is true ; if both are false , returns false . | a || b |
! | Logical NOT: false if the operand is true and vice versa | !a |
Example 3: Logical operators in Javascript
// logical AND
console.log( true && true ); // returns true
console.log( true && false ); // returns false
// logical OR
console.log( true || false ); // returns true
console.log( false || false ); // returns false
// logical NOT
console.log( !true ); // returns false
console.log( !false ); // returns true
Bitwise Operators in Javascript
Bitwise operators perform their operations on the binary representation of operands. For example, the binary representation of operand 9
is 1001
.
Operator | Description | Example |
---|---|---|
& | Bitwise AND | a & b |
| | Bitwise OR | a | b |
^ | Bitwise XOR | a ^ b |
~ | Bitwise NOT | ~ a |
<< | Left Shift | a << b |
>> | Sign-propagating right shift | a >> b |
>>> | Zero-fill right shift | a >>> b |
String Operators in Javascript
+
operator in javascript is used with both numbers and strings. If we use +
operator with numbers it performs addition and if we use it with strings it performs string concatenation.
Example 4: String operators in Javascript
// string concatenate
console.log('hello' + 'world');
// helloworld
let x = 'coding';
x += 'claw'; // x = x + 'claw';
console.log(x);
// codingclaw
Output
helloworld
codingclaw
Some other operators in Javascript
Here is the list of some other javascript operators.
Operator | Description | Example |
---|---|---|
, | evaluates each of its operands and returns the value of the last operand. | let a = ( 1, 2, 3 ); // 3 |
?: | returns values based on a condition | ( 4 > 2 ) ? ‘yes’ : ‘no’; // yes |
delete | deletes an object’s property or an array element | delete object.property; |
typeof | returns a string indicating the type of the unevaluated operand. | typeof 2; // “number” |
void | specifies an expression to be evaluated without returning a value | void (expression) |
in | returns true if the specified property is in the specified object | prop in object |
instanceof | returns true if the specified object is of the specified object type | object instanceof objectType |