Logical Operators



index
Disabled back button Next Section
printable version

Section 1: Overview

The logical operators are && (And), || (Or), and ! (Not).

Truth tables.
Section 2: Proper Procedure

&& and || can only be used to test a complete condition.

if (( testCondition == 1 ) || ( testCondition == 2 ) || ( testCondition == 4 ))

The following is an invalid statement:

if (testCondition == 1 || 2 || 4)

The same holds true for the &&. The following is invalid:

if ( grade < 90 && >= 80 )

Similarly, you cannot write a statement like

if ( 90 < grade <= 80 )

The correct syntax for such a statement is

if (( grade < 90 ) && ( grade >= 80 ))

Section 3: Short-Circuit Evaluation

As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:

The rules of logic guarantee that these evaluations are always correct.

Short circuit truth tables.
Section 4: Precedence of JavaScript Logical Operators
Precedence of Logical Operators
Section 5: Precedence of JavaScript Operators

A complete table of operator precedence can be found here.

Precedence of Operators
Section 6: Resources