MSc-IT Study Material
June 2010 Edition

Computer Science Department, University of Cape Town

Boolean operators

So far we have used the window.confirm() method that is guaranteed to return either true or false. In a more general case, you will obtain true / false values by comparing two or more values.

Of course, you need to be able to compare all kinds of values and make a variety of comparisons. Here are the main operators for doing, which should be familiar to you from the Java module:

OperatorExampleMeaning
==a == bequality between any two values; returns true or false (example tests for a being equal to b)
!=a != binequality between any two values; returns true or false (example tests for a not being equal to b)
===a === b identity between any two objects; returns true or false (example tests to see if a refers to the same object as b)
!==a !== bnon-identity between any two objects; returns true or false (example tests to see if a does not refer to the same object as b)
<a < bless than between any two values; returns true or false (example tests for a being less than b)
<=a <= bless than or equal to between any two values; returns true or false (example tests for a being less than or equal to b)
>a > bgreater than between any two values; returns true or false (example tests for a being greater than b)
>=a >= bgreater than or equal to between any two values; returns true or false (example tests for a being greater than or equal to b)
&&a && blogical 'and' between two Boolean (true/false) values (example returns true only if both a and b are true, and false otherwise)
||a || blogical 'or' between two Boolean (true/false) values (example returns false only if both a and b are false, and true otherwise)
!!alogical 'not' of one Boolean (true/false) value (example returns false if a is true and true if a is false)