DevLearningTools

๐Ÿšง This site is under active construction โ€” new tools, guides, and pages are added every week.

MODULE 3 ยท LESSON 05

Ternary Operator

Writing compact conditional assignments in ColdFusion with the ternary operator (condition ? trueValue : falseValue) in CFScript, plus the iif() alternative for Tag Syntax.

New lessons are added one at a time as the course gets built out โ€” a graded quiz for each lesson is still on the way.

Some if...else statements exist only to assign one of two values to a variable. Writing four or five lines for that is more ceremony than the logic needs. The ternary operator compresses a simple if...else into a single expression.

By the end of this lesson, you'll know how to use the ternary operator in CFScript, the iif() alternative available in Tag Syntax, and when a ternary is the wrong choice.

Learning Objectives

After completing this lesson, you'll be able to:

  • Write a ternary expression in CFScript.
  • Understand the condition ? trueValue : falseValue syntax.
  • Use iif() as a Tag-Syntax-friendly alternative.
  • Recognize when a ternary hurts readability instead of helping it.

Ternary Operator Syntax

The ternary operator takes a condition and two values: the one to use if the condition is true, and the one to use if it's false.

CFScript
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.
NOTE

This is equivalent to: if (age >= 18) { status = "Adult"; } else { status = "Minor"; } โ€” just shorter.

The Tag Syntax Alternative: iif()

Tag Syntax has no direct ternary symbol, but the iif() function does the same job and works in both CFScript and Tag Syntax.

Tag Syntax
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.
NOTE

Wrap the second and third arguments in DE() (Delay Evaluation). Without it, ColdFusion evaluates both the true and false expressions before checking the condition โ€” which can throw errors or waste work if one branch is expensive.

Real-World Example: Membership Discount

CFScript
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.

Common Beginner Mistakes

Nesting Ternaries Inside Ternaries

status = (score >= 90) ? "A" : (score >= 80) ? "B" : "C"; works, but it's hard to read at a glance. For more than two outcomes, an else if chain is usually clearer.

Using a Ternary for Side Effects

A ternary is meant to produce a value, not to run unrelated statements in each branch. If either branch needs to do more than return a value, use a regular if...else.

Forgetting DE() with iif()

Without DE(), both the true and false expressions in iif() are evaluated up front, even the one that isn't used โ€” which can throw an error if that branch isn't actually valid for the current data.

Best Practices

  • Use a ternary only to assign a simple value, not to run multi-step logic.
  • Avoid nesting more than one ternary โ€” switch to else if for three or more outcomes.
  • Always wrap iif()'s value arguments in DE() unless they're plain literals.
  • Prefer if...else when either branch does more than produce a single value.

Interview Questions

What is the syntax of the ternary operator in CFScript?

condition ? valueIfTrue : valueIfFalse

What is the Tag-Syntax-friendly equivalent of the ternary operator?

The iif() function.

Why should iif()'s arguments be wrapped in DE()?

Without DE(), ColdFusion evaluates both the true and false expressions before deciding which to return, which can cause errors or unnecessary work.

When should you avoid using a ternary operator?

When a branch needs to run more than one statement, or when nesting ternaries would hurt readability โ€” use if...else or else if instead.

Summary

In this lesson, you learned how to write compact conditional assignments with the ternary operator in CFScript, used iif() with DE() as the Tag Syntax alternative, and saw when a ternary makes code clearer versus when it should be replaced with a full if...else.

This completes the Decision Making module. You now know how to run code conditionally with if, if...else, nested if, switch, and the ternary operator, in both CFScript and Tag Syntax.

What's Next?

The next module covers Loops โ€” starting with the for loop, which repeats a block of code a set number of times.