In this article, you will learn about JavaScript operators with the help of examples and also you will learn types of JavaScript operators. Before going to further this tutorial, we will know little about operators.
Headings of Contents
What is operator ?
In JavaScript, an operator is a special type of symbol that is used to perform operations on operands. Operands can be values and variables.
Let’s take a simple example to understand operator in JavaScript very well.
Example:
10 - 5 + 10
In above example, – and + are the operator and 10, 5 and 10 are the values.
Let’s see how many types of JavaScript operators available.
Types of JavaScript Operators
Here list of various types of JavaScript operators you will learn in this tutorial.
- Assignment Operator
- Arithmetic Operator
- Comparison Operator
- Logical operator
- Bitwise operator
- String operator
JavaScript Assignment Operator
JavaScript assignment operators are used to assign the value to the variable.
Example
var x = 100;
In above example, = is the assignment operator that is used to assign the value 100 to the variable x.
Here list of some commonly used assignment operators:
Operator | Name | Example |
---|---|---|
= | Assignment Operator | x = 5; |
+= | Addition assignment | x += 5; // x = x+5 |
-= | Subtraction assignment | x -= 5; // x = x – 5 |
*= | Multiplication assignment | x *= 10; // x = x * 10 |
/= | Division assignment | x /= 20; // x = x / 20 |
%= | Remainder assignment | x %= 10; // x = x % 10 |
**= | Exponentiation assignment | x **= 10; // x = x ** 10 |
JavaScript Arithmetic Operators
JavaScript arithmetic operators are used to perform arithmetic operations.
Example
var result = 100 + 50 - 35
//result is:- 115
In above example + operator is used to add 100 and 50 , and – operator is used to subtract 35 from 150.
Here list of some common used arithmetic operators:
Operator | Name | Example |
---|---|---|
+ | Addition | x + y |
– | Subtraction | x – y |
* | Multiplication | x * y |
/ | Division | x / y |
% | Remainder | x % y |
++ | Increment | x ++, ++x |
— | Decrement | x –, –x |
** | Exponentiation | x ** y |
Example
var x = 20, y = 10;
// Addition
console.log('x + y :- ', x + y); // 30
// Subtraction
console.log('x - y :- ', x - y); // 10
// Multiplication
console.log('x * y :- ', x * y); // 200
// Divide
console.log('x / y :- ', x / y); // 2
// Remainder
console.log('x % y :-', x % y); // 0
// Increment
console.log('++x :- ', ++x); // x is now 21
console.log('x++ :-', x++ ); // print 21 and then increased 22
console.log('x :- ', x); // 22
// Decrement
console.log('--y :- ', --y); // y is now 9
console.log('y-- :-', y--); // print 9 and then decreased 8
console.log('y :- ', y); // 8
JavaScript Comparison Operators
JavaScript Comparison operators are used to compare two value and always returns a Boolean value, either true or false.
Example
var x = 20, y = 10;
console.log(x > y);
As you know in above example, 20 is greater than 10,that’s why result will be true.
Here list of some common used comparison operators:
Operator | Name | Example |
---|---|---|
== | Equal to | x == y |
!= | Not equal to | x != y |
=== | Strict equal to | x === y |
!== | Not equal to | x !== y |
> | Greater than | x > y |
>= | Greater than or equal to | x >= y |
< | Less than | x < y |
<= | Less than or equal to | x <= y |
Example
var x = 20, y = 10, z = 10;
// Equal to
console.log('y = z :- ', y = z); // true
// Not equal to
console.log('x != y :- ', x != y); // true
// Greater Than
console.log('x > y:- ', x > y) // true
// Less Than
console.log('y < x:- ', y < x) // false
//Greater Than or equal to
console.log('y >= x :- ', y >= x) // true
// Strict equal to
console.log(10 === 10); // true
// Strict not equal to
console.log(10 !== 10); // false
JavaScript Logical Operators
Logical operators are used to perform logical operation and return a Boolean value, either true or false.
Example
var x = 12, y = 20;
var result = (x > 10) && (y < 30);
console.log(result)
Here list of some common used logical operators:
Operator | Name | Example |
---|---|---|
&& | Logical AND | x && y |
|| | Logical OR | x || y |
! | Logical NOT | !x |
Example
// Logical AND
console.log(true && false); // false
console.log(true && true); // true
// Logical OR
console.log(true || true); // true
console.log(true || false); // true
// Logical NOT
console.log(!true)
console.log(!false)
JavaScript Bitwise Operators
JavaScript Bitwise Operators are used to perform operations on binary representation of the numbers.
Operator | Description |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | Left Shift |
>> | Sign-propagating right shift |
>>> | Zero fill right shift |
JavaScript String Operator
In JavaScript, + operator can also used to concatenate two or more strings.
Example
var x = 'Programming', y = ' Funda';
console.log(x + y)
Conclusion
In this tutorial, you have learned all about operators and types of JavaScript operators available with the help of examples. I think you don’t have any confusion regarding JavaScript Operators after this article.
Operators are very useful when you want to perfom operations in a JavaScript program.You can perform different-2 operations with the help of different-2 operators.
If you like this JavaScript operators article, please share and keep visiting for further JavaScript tutorials.
Reference:- Click Here