JavaScript Tutorial

adplus-dvertising
Operators In JavaScript
Previous Home Next

JavaScript operators : are symbols that are used to perform operations on operands foe example 4 + 7 is equal to 11. Here 4 and 7 are called operands and + is called operator.

There are following types of operators used in JavaScript :

  • Arithmetic Operators
  • Logical Operators
  • Comparison Operators (Relational)
  • Bitwise Operators
  • Assignment Operators
  • Special Operators

Airthematic operator

Arithmetic operators are used to perform arithmetic operations on the operands

Operator Description Example
+ Addition 20+20 = 40
- Subtraction 30-10 = 20
* Multiplication 10*10 = 100
/ Division 20/10 = 2
% Modulus (Remainder) 20%10 = 0
++ Increment var a=15; a++; Now a = 16
-- Decrement var a=15; a--; Now a = 14
<html>
<body>
<h1>The + Operator</h1>
<script>
var x = 5;
var y = 2;
var z = x + y;
document.write("the sum of" +z)
</script>
</body>
</html>

Output :

The + Operator the sum of 7

Logical Operators

Operator Description Example
&& Logical AND (10==20 && 20==33) = false
|| Logical OR (10==20 || 20==33) = false
! Logical Not !(10==20) = true

Comparison Operators or ( Relational Operators )

Operator Description Example
== if the value of two operands are equal or not, 10==40 = false
=== Identical (equal and of same type) 10==20 = false
!= Not equal to 10 !=20 = true
!== Not Identical 20 !== 20 = false
> Greater than 20 > 5 = true
>= Greater than or equal to 50 >= 10 = true
< Less than 20 < 10 = false
<= Less than or equal to 80 <=10 = false

Bitwise Operators

Operator Description Example
& Bitwise AND operator (a==10 & b==33) = false result
| Bitwise OR operator (a==x | 20==33) = false result
^ Bitwise XOR operator (a==8 ^ b==15) = false result
~ Bitwise NOT operator (~30) = -30
<< Bitwise Left Shift operator (10 << 2) = 40
>> Bitwise Right Shift operator (10>>2) = 2
>>> Bitwise Right Shift with Zero (10>>>2) = 2

Assignment Operators

Operator Description Example
= Assign A=10;B= 20;
A+B=30
+= Add and assign x=50; x+=20;
Now x = 70
- = Subtract and assign A=30; A-=10;
Now A= 20
*= Multiply and assign A=5; A*=30;
Now A = 150
/= Divide and assign x=20; x/=2;
Now x = 10
%= Modulus and assign x=100; x%=2;
Now a = 0

Special Operators

Operator Description Example
? Conditional Operator Lets you perform a simple "if...then...else"
, Comma Operator Evaluates two expressions and returns the result of the second expression.
delete Delete Operator its use to deletes a property from the object.
new new operator its creates an instance (object)
typeof typeof operator its Returns a string thats indicate the type of the unevaluated operand.
void void operator it is specifies an expression to be evaluated without returning a value.
this this operator its Returns a string that's indicating the type of the unevaluated operand.
in in oparator Its checks if object has the given property
yield yield operator its checks what is returned in a generator by the generator's iterator.
Previous Home Next