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false.

Comparison Operators

Comparison operators are used in logical statements to determine equality or difference between variables or values.

Given that x=5, the table below explains the comparison operators:

Operator Description Example
== is equal to x==8 is false
=== is exactly equal to (value and type) x===5 is true
x==="5" is false
!= is not equal x!=8 is true
> is greater than x>8 is false
< is less than x<8 is true
>= is greater than or equal to x>=8 is false
<= is less than or equal to x<=8 is true

How Can it be Used

Comparison operators can be used in conditional statements to compare values and take action depending on the result:

if (age<18) document.write("Too young");

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

Logical Operators

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

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

Operator Description Example
&& and (x < 10 && y > 1) is true
|| or (x==5 || y==5) is false
! not !(x==y) is true

Conditional Operator

JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.




greeting=(visitor=="PRES")?"Dear President ":"Dear ";

If the variable visitor has the value of "PRES", then the variable greeting will be assigned the value "Dear President " else it will be assigned "Dear".

W]s8}3~6biH ٤ iw2r-B?~dB jX:WGHWp9D*tqMȗbN㜦+.RrVT#d>7C2&:գV@{c UPrAÛ@ Xk^gYĬo%4yk-8Mc=s)P !z 3Z-)7B!˜Ώ9bO؇r;nia:op* xKV2tXniL-2tnVx>>٩u֞iRsUkWB5ޕU gB E r/+;eO+K_N,1K5yfE"1Z u1[iR4|#M֣kKZ9Z#~%E~wq 3 m:rګ;Oa3 K/y5nD\*J5α߆'`ĿF