Menu Close

JavaScript Operators

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.

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:

OperatorNameExample
=Assignment Operatorx = 5;
+=Addition assignmentx += 5; // x = x+5
-=Subtraction assignmentx -= 5; // x = x – 5
*=Multiplication assignmentx *= 10; // x = x * 10
/=Division assignmentx /= 20; // x = x / 20
%=Remainder assignmentx %= 10; // x = x % 10
**=Exponentiation assignmentx **= 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:

OperatorNameExample
+Additionx + y
Subtractionx – y
*Multiplicationx * y
/Divisionx / y
%Remainderx % y
++Incrementx ++, ++x
Decrementx –, –x
**Exponentiationx ** 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:

OperatorNameExample
==Equal tox == y
!=Not equal tox != y
===Strict equal tox === y
!==Not equal tox !== y
>Greater thanx > y
>=Greater than or equal tox >= y
<Less than x < y
<=Less than or equal tox <= 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:

OperatorNameExample
&&Logical ANDx && y
||Logical ORx || 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.

OperatorDescription
&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

JavaScript Assignment Operators
JavaScript Arithmetic Operators

Related Posts