JavaScript Comparison and Logical Operators


Comparison Operators (given x = 5)

Operator Description Example
== is equal to 5 == 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 5 > 8 is false
< is less than 5 < 8 is true
>= is greater than or equal to 5 >= 8 is false
<= is less than or equal to 5 <= 8 is true

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

Conditional Operator

 var1 = ( cond ) ? val1 : val2;

A conditional operator assigns a value to a variable based on some condition. For the script, if the OK button is clicked, then go back to the default homepage. Otherwise, go back to the current page.



   

The method home of the object window points the browser to the URL specified in preferences as the user’s home page; equivalent to the user pressing the browser’s Home button.


Demonstration
The following demonstration shows how the script of HTML and JavaScript is displayed on the Web.

     





      Any authentic work of art must start an argument between the artist and his audience.    
      — Rebecca West