JavaScript Comparison and Logical Operators

Comparison and logical operators are used to test true or false .

Comparison operators

Comparison operators are used in logic statements to determine whether variables or values are equal.

x=5, the following table explains the comparison operators:

OperatorDescriptionComparisonreturn valueTry it!
==equalx==8falseTry It!
x==5trueTry It!
===Absolutely equal (both value and type are equal)x==="5"falseTry It!
x===5trueTry It!
!= not equal tox!=8trueTry It!
!== Not absolutely equal (one of the value and type is not equal, or both are not equal)x!=="5"trueTry It!
x!==5falseTry It!
> more than thex>8falseTry It!
< Less thanx<8trueTry It!
>= greater than or equal tox>=8falseTry It!
<= less than or equal tox<=8trueTry It!

How to Use

You can use comparison operators in conditional statements to compare values, and then take actions based on the results:

if (age<18) x="Too young";

You will learn more about conditional statements in the next section of this tutorial.

Logical Operators

Logical operators are used to determine the logic between variables or values.

Given x=6 and y=3, the following table explains the logical operators:

&&and(x < 10 && y > 1) : true
||or(x==5 || y==5) : false
!not!(x==y) : true

Conditional Operator

JavaScript also contains conditional operators that assign values to variables based on certain conditions.


variablename = (condition) ? value1 : value2


If the value in the variable age is less than 18, assign the value "age too young" to the variable voteable, otherwise assign the value "age reached".

voteable=(age<18)? "Age is too young": "Age has reached";