- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
How Ternary Operators Can Simplify Your JavaScript Logic
Write less code without sacrificing clarity. In this lab, you'll work with a cinema ticket ordering application and refactor its conditional logic from verbose if/else blocks to concise ternary expressions. You'll learn to recognize the three-part structure of a ternary operator, apply it to real utility functions, and build judgment for when this pattern makes code cleaner.
Lab Info
Table of Contents
-
Challenge
Step 1: Introduction
Conditional logic is at the heart of most JavaScript programs, and the way you write that logic affects how readable and maintainable your code becomes.
In this lab, you'll work with a cinema ticket ordering application made up of two modules:
ticketPricing.js: calculates prices and shippingorderDisplay.js: generates status labels and formatted totals
Your goal is to refactor each function from a verbose
if/elseblock into an equivalent ternary expression, building an intuition for when this concise syntax makes code clearer.Both modules in the
application/directory are fully functional. Theif/elselogic is correct, but a previous developer wrote each condition with multiple lines when a single line would do.Take a moment to open
src/ticketPricing.jsandsrc/orderDisplay.jsto get a sense of the existing structure before moving on to the first task. info> This lab experience was developed by the Pluralsight team using an internally developed AI tool. All sections were verified by human experts for accuracy prior to publication. However, content may still contain errors or inaccuracies, and we recommend independent verification.
To report a problem or provide feedback, click here. Feedback may be used to improve accuracy in accordance with our Privacy Policy. -
Challenge
Step 2: Simplifying Conditional Assignments
You'll start with
src/ticketPricing.js, which contains two functions that control pricing decisions.Both functions use an
if/elseblock to return one of two values based on a condition. This pattern that maps directly onto the ternary operator's condition, true-value, and false-value structure. Converting them will reduce each function to a single return statement. -
Challenge
Step 3: Ternary Operators in Action
With the pricing functions refactored,
ticketPricing.jsnow expresses both conditions on a single line each.You'll apply the same technique to
src/orderDisplay.js, which handles the visual side of an order. These functions work with strings rather than numbers, giving you practice with ternary expressions in a different context.After converting both functions, you'll run the demo script to see all four refactored functions produce a formatted order summary in the Terminal.
-
Challenge
Step 4: Conclusion
You've refactored all four utility functions in the cinema ticket ordering application from
if/elseblocks to ternary expressions. Working throughticketPricing.jsandorderDisplay.js, you applied the ternary operator's three-part structure: condition, true value, and false value. You applied that structure to boolean flags, comparison operators, and string equality checks.The final
npm run demorun confirmed that the refactored functions produce the same correct output as the original code, demonstrating how ternary operators can reduce lines of code while preserving behavior and readability.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.