Go to: Articles List

How to use logical operators (||, &&, !)

Here is a some simple code to demonstrate the power and usefulness of the logical operators. If you are comparing several variables, like in form validation, it is helpful to use these. Change the values of the variables to see their effect.

|| = or
&& = and
! = not (!myvar returns false if myvar is true, returns true if myvar is false, in other words -- it makes it do the opposite of the normal if condition)

var1 = true
var2 = false
var3 = false
var4 = false

if ((var1 || var2) && (var3 || var4)) {
alert("got inside...");
} else alert("got ELSE...");