Elvis operator

The support has been provided in ColdFusion for the Elvis operator (?:). The Elvis operator is primarily used to assign the ‘right default’ for a variable or an expression.

It returns the resultant value of its left operand's expression, if that resultant value is defined and not null and not a false value. Otherwise, it returns the resultant value of its right operand's expression (the default value).

For instance,

myDisplayName = userName ?: “Anonymous”;

In the above example, if userName is defined and not null and not a false value, it will be assigned to the myDisplayName variable. Otherwise, the value "Anonymous" will be assigned to the myDisplayName variable.

See the following example:

employeeName = getEmployeeName(ID) ?: “Joe”;

In the above example, if getEmployeeName(ID) does not return any value, or if the returned value is a false value, the value "Joe" will be assigned to the employeeName variable.

Similiarly, you can use this operator for Struct:

securityNumber = securityStruct[‘Joe’] ?: “”;

Get help faster and easier

New user?