Whatterz


Using JavaScript Logical Operators in ColdFusion 8

by Simon. Average Reading Time: about a minute.

Following on from my posts on Arithmetic and Assignment operators, Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.

The logical operators are described in the following table:

Operator Usage Description
Logical AND (&&) expr1 && expr2 Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.
Logical OR (||) expr1 || expr2 Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns false.
Logical NOT (!) !expr Returns false if its single operand can be converted to true; otherwise, returns true.

Using ColdFusion, the Logical operators can be expressed as follows:

Logical AND

In the below example, both operands, x and y, return true, therefore myVariable is set to true within the logical statement.

<cfscript>
x = 9;
y = 2;
myVariable = false;
 
if (x < 10 && y > 1)
{
	myVariable = true;
}
</cfscript>

Logical AND Operator Example Results

Logical OR

In the below example, neither x equals 10 nor y equals 1, therefore the first part of the logical statement is not resolved. Instead, the else statement is processed, assigning the boolean value false to the variable myVariable.

<cfscript>
x = 9;
y = 2;
 
if (x == 10 || y == 1)
{
	myVariable = true;
}
else
{
	myVariable = false;
}
</cfscript>

Logical OR Operator Example Results

Logical NOT

In the below example, x does not equal y. Ordinarily this would result in the logical statement being evaluated, however, a logical negation has been applied to the beginning of the expression to be evaluated, therefore the variable myVariable is assigned the boolean value true.

<cfscript>
x = 9;
y = 2;
myVariable = false;
 
if (!x==y)
{
	myVariable = true;
}
</cfscript>

Logical NOT Operator Example Results

This article has been tagged

, , , , ,

Other articles I recommend

Using JavaScript Comparison Operators in ColdFusion 8

Following on from the first three parts of the “Using JavaScript Operators” in ColdFusion 8, where I introduced Arithmetic, Assignment and Logical Operators, it is now the turn of Comparison Operators.

Using JavaScript Arithmetic Operators in ColdFusion 8

An often mentioned complaint by ColdFusion developers is the lack of operators commonly found in other programming languages such as JavaScript. For example, instead of the greater-than (>) symbol we have been restricted to the more wordy GT or GREATER THAN operator. However, in ColdFusion 8 this has changed and we have more freedom to use familiar JavaScript operators in <cfscript> blocks.

In the following series of posts, I will introduce the changes and show some simple examples. The first in the series is Arithmetic Operators.

Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/).

These operators work as they do in most other programming languages.

Using JavaScript Assignment Operators in ColdFusion 8

In my first post in this series, I introduced Using JavaScript Arithmetic Operators in ColdFusion 8. Now we’ll concentrate on Assignment operators.

An assignment operator assigns a value to its left operand based on the value of its right operand.

The basic assignment operator is equal (=), which assigns the value of its right operand to its left operand. That is, x = y assigns the value of y to x. The other assignment operators are usually shorthand for standard operations, as shown in the following table.