The Boolean operators are used to compare two statements and direct the further code execution flow.

They are the most important operators and often used with if...else clause and similar statements.

There are three logical or Boolean operators; these are NOT, AND and OR.

Logical NOT

The logical NOT is represented by an exclamation point symbol ("!") and it may be applied to any value in JS. The result is always a Boolean value and it depends on the data type used in the logical operation.

It works in a way that it first converts the operand in a Boolean value and then it negates it. The conversion of an operand value into a Boolean works under the following rules:

  • If the operand is an object, false is returned;
  • If the operand is an empty string, true is returned;
  • If the operand is a non-empty string, false is returned;
  • If the operand is a number "0", true is returned;
  • If the operand is any number but "0", false is returned;
  • If the operand is null, true is returned;
  • If the operand is NaN, true is returned;
  • If the operand is undefined, true is returned.

In the following examples a typical NOT behavior is shown:

alert(!false); //true

alert(!"name"); //false

alert(!""); //true

alert(!null); //true

alert(!100); //false

If double ! is used in a row (!!). then the operation converts the operands value into its real Boolean equivavalent. For instance:

alert(!!100); //tru3

alert(!!null); //false

The first NOT returns a Boolean value as it normally does, while the seconds NOT negates the value giving the true Boolean value of the variable. This technique simulates the usage of the Boolean() function.

Logical AND

The logical AND is represented by double ampersand symbol ("&&") and it is applied to two values. The result will be "true" only if both values are "true" as shown in the table below:

Logical AND
value 1 value 2 result
true true true
true false false
false true false
false false false

The Logical AND works with other operand values, beside the Boolean ones, and it does that by following rules:

  • If the 1st operand is an object, then the 2nd operand is always returned;
  • If the 2nd operand is an object, then the object is returned only if the first operand evaluates to true;
  • If both operands are objects, then the 2nd operand is returned;
  • If either operand is null, then null is returned;
  • If either operand is NaN, then NaN is returned;
  • If either operand is undefined, then undefined is returned.

With the AND operation the first operand is crucial for the result of the whole evaluation; meaning that if the first operand is in the state of Boolean false, the second will never be evaluated. Consider this example:

var enabled = false;

if (enabled && (5 > 3)) {

   alert(Hello!);

}

In the example above the pop-up alert box will never be shown. Although the second operand is true (5 > 3), the first one isn't (enabled = false).

Logical OR

The logical OR is represented by double pipe symbol ("||") and it is applied to two values. The result will be "true" if any of the values is "true" as shown in the table below:

Logical AND
value 1 value 2 result
true true true
true false true
false true true
false false false

.The Logical OR works with other operand values, beside the Boolean ones, and it does that by following rules:

  • If the 1st operand is an object, then the 2nd operand is returned;
  • If the 1st operand is evaluates to false, then the 2nd operand is returned;
  • If both operands are objects, then the 1st is returned;
  • If both operands are null, thennull is returned;
  • If both operands are NaN, then NaN is returned;
  • If both operands are undefined, then undefined is returned.

Same as with the AND operators, the OR operation depends heavily on the first operand; thus if the first operands evaluates to Boolean true, the second will not be evaluated. Consider this example:

var enabled = true;

if (enabled || (5 > 3)) {

   alert(Hello!);

}

In the example above the pop-up alert box will appear regardless to the result of the second operand, that is due the fact that the first operand is already evaluated to true.

Example

The example with Boolean (logical) operators in JavaScript:

 

›› go to examples ››