Top » TUTORIALS » IF If you find this page useful
please make a secure donation
My Account  |  Cart Contents  |  Checkout   

Javascript Reserved Word - IF

IF statments can be thought of as the fundamental control statements. It allows the code to be executed IF and only IF the logic in the expression yields a boolean true value.

The basic syntax for an IF statement:

if (expression)
[{ [code;]+ }] | [code;]

Breaking this down a bit:
expression is any valid boolean-like statement. Since javascript does not have any real types and all things can be cast into any other real thing, it is necessary to state what really equates to a boolean false value.
undefined, null, 0, false
All other things equate to a boolean true statement.

Very simple IF statements that output "It was True".

if (1) document.write("It was True");
if (!0) document.write("It was True");
Note that "!" is "not" - so "not zero" equates to boolean true.
if (1 == 1) document.write("It was True");
Note that "==" is the "is it equals to" condition.

For more complicated compoud expressions:
if (1 == 1 && 0 == 0) document.write("It was True");
Note this reads as: one is equal to one AND zero is equal to zero.
if (1 == 0 || 0 == 0) document.write("It was True");
Note this reads as: one is equal to zero OR zero is equal to zero.