Operators

Javascript Operators are used to operate tasks like assign, addition, subtract, compare values, etc. JavaScript has arithmetic, logical, assignment and comparison operators.

JavaScript has both binary and unary operator including one ternary operator (conditional operator).

Binary operator

Binary operators required two operands, one before and one after operator. x+y=z

x (operand) +(operator)  y(operand)

Unary Operator

Unary operators required only one operand, either before or after operator. i++

x++
++x

Ternary Operator

Ternary Operator is conditional operator in javaScript witch use three operands. For Example, (3>2) ? console.log("yes"): console.log("no") will print yes and (1>2) ? console.log("yes"): console.log("no") will print no.

yes
(3>2) ? console.log("yes"): console.log("no");
no
(1>2) ? console.log("yes"): console.log("no");

Type of Operators in Javascript



Arithmetic Operators

An Arithmetic operator is used to perform Arithmetic operations between values. Like Addition, Subtraction, Multiplication, Division etc

Arithmetic operators in JavaScript

OperatorDescriptionExample
+ Addition 2+3=5
"2" + 3 = "23"
- Subtraction 5-3=2
"5" - 3 = 2
* Multiply 2*3=6
/ Divide 6/3=2
% Remainder 6%3=0
++ Increment, y++ means y = y+1 var y=2; ++y; y=3
-- Decrement, y-- means y = y-1 var y=2; --y; y=1
** Exponentiation Operator 2**3 returns 8

The ** Exponentiation Operator was introduced in JS ES7. There is no support for Exponentiation Operator in IE browser, chrome<52 and Firefox<52


Logical Operators

Logical Operators are used to check logic between two operators. and (&&), or (||) and not (!) are logical operators.

Logical Operators in JavaScript

OperatorDescriptionExample
&& and, when both are correct 2 < 5 && 2> 1 is true
|| or, when any one is correct var x=2, 2>5 || x==2 is true
! not !(2==3) is true

Assignment Operators

Assignment operators ars used to assign some value to js variables. =, +=, -=, *=, /= are all assignment operators in javascript.

Assignment Operators in JavaScript

OperatorDescriptionExample
= Assignment x=2; means x is 2
+= Addition Assignment var x=2; x+=2 means x=x+2
-= Subtraction Assignment var x=2; x-=2 means x=x-2
*= Multiplication Assignment var x=2; x*=2 means x=x*2
/= Division Assignment var x=2; x/=2 means x=x/2

Comparison Operators

Comparison operators are used in a statement to compare two values. Like == is used to compare values but === is used to compare values and datatypes.

Comparison Operators in JavaScript

OperatorDescriptionExample
== Equal to 2 == "2" is true
true == 1 is true
undefined == null is true
=== Strict equal to 2==="2" is false
true === 1 is false
undefined === null is false
!= not equal 2!=1 is true
!== not strict equal 2!=="2" is true
> greater than 2> 5 is false,
& 2 <5 is true
>= greater than or equal to 3>=3 is true
< less than 1< 3 is true
<= less than or equal to 2<=2 is true

Video