JavaScript Operator

An operator is used to perform specific mathematical or logical manipulations.

JavaScript has following types of operators.

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Conditional Operators

JavaScript Arithmetic Operators

JavaScript arithmetic operators are used to perform arithmetic manipulations on numbers.A list of arithmetic operators is given below.

Operator Description Example
+ Perform Addition Operation between two operands 10 + 2 =12
- Subtracts 10 -2 =10
* Multiply two operands 10*2 =20
/ Divide numerator by denumerator 10/2 =20
% Return remainder of after an integer division 10%3=1
++ Increases integer value by one 10++=11
-- Decreases integer value by one 10--=9

JavaScript Assignment Operators

JavaScript assignment operators assign value of right operand to its left operand.A list of assignment operators is given below.

Operator Shorthand Same As
= m=n m=n
+= m +=n m = m + n
*= m *= n m = m * n
/= m/=n m =m / n
%= m %=n m = m % n
<<= m <<= n m = m << n
>>= m >>= n m = m >> n

JavaScript Comparison Operators

A list of comparison operators is given below.Consider variable m holds 10 and variable n holds 2.

Operator Description Example
== equal to (m == n) is not true.
=== equal value and equal type (m === n) is not true.
!= not equal to (m != n) is true.
!== not equal value or not equal type (m !== n) is true.
> greater than (m > n) is true.
>= greater than or equal to (m >= n) is true.
<= less than or equal to (m <= n) is not true.

JavaScript Logical Operators

A list of logical operators is given below.Consider variable m holds 10 and variable n holds 2.

Operator Description Example
&& Logical AND (m && n) is true.
|| Logical OR (m || n) is true.
! Logical NOT !(m && n) is false.

Example of Logical Operators

Logical AND Logical OR Logical NOT
true && true; //will retrun true true ||true; //will retrun true !true ; //will retrun false
true && false; //will retrun false true || false; //will retrun true !false ; //will retrun true
false && false; //will retrun false false || false; //will retrun false !false ; //will retrun true

JavaScript Conditional Operators

JavaScript Conditional Operators takes three operands.

Syntax

condition ? value1 : value2

Example

var result = (mark >= 33) ? "pass" : "fail";

Above statement assigns the value "pass" to the variable result if mark is 33 or more. Otherwise, it assigns the value "fail" to result.