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. In an expression, if the resultant value is not defined, then the object will be assigned to the left most part of the expression otherwise a default value (define at the right most part) will be assigned.


For instance,

myDisplayName = userName ?: “Anonymous”;

In the above example, if userName is defined, it will be assigned to the myDisplayName variable. If the userName is not defined, 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, the value “Joe” will be assigned to the employeeName variable.

Similiarly, you can use this operator for Struct:

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

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy