Choosing a path for product rules
This lesson applies Conditions concepts to code you can run and adapt.
✓ What you will learn in this topic
- Explain key Conditions concepts.
- Read and adapt the examples.
- Apply the idea to a small real interface or data task.
Concept explanation
if, else, else if, nested choices, switch, truthy/falsy values, and real product decisions.
These concepts appear in everyday interfaces and data transformations; understanding the trade-offs helps you choose a reliable implementation.
Syntax
The examples use standard ECMAScript syntax. Run console examples in a browser console or Node.js; DOM examples require a browser page containing the referenced elements.
Example 1: a small experiment
Run this compact example to observe the core rule before applying it to a larger feature.
const role = 'editor';
if (role === 'admin') console.log('Manage users');
else if (role === 'editor') console.log('Edit articles');
else console.log('Read only');Expected output: Edit articles
Why it is useful: A small experiment makes the value or behavior easy to inspect.
Example 2: a practical feature
This example models a small piece of application behavior. Adapt its values and inspect what changes.
const stock = 0;
console.log(stock ? 'In stock' : 'Sold out');Expected output: Sold out
Why it is useful: The same pattern can be extended into a real feature without tying the entire application to one large block of code.
Common mistakes
An empty array and empty object are truthy in JavaScript. Check array length when you mean âhas items.â
Best practices
Use conditions that describe business rules clearly; use switch for a small set of discrete values.
Practice exercise
Add a guest role with read-only access and test each branch.
Lesson summary
if, else, else if, nested choices, switch, truthy/falsy values, and real product decisions.
Knowledge check
Which practice best avoids the common mistake in Conditions?
Show explanation
Use conditions that describe business rules clearly; use switch for a small set of discrete values.
Practical example
javascriptconst stock = 0;
console.log(stock ? 'In stock' : 'Sold out');
Expected result:
Sold out
Hands-on practice task
Add a guest role with read-only access and test each branch.
In this section
Other lessons in Conditions
Broaden your knowledge
Related learning topics
CSS
Learn to style, lay out, and adapt interfaces with a complete course from CSS foundations through advanced responsive architecture.
HTML
Learn to structure accessible, meaningful web pages with a complete HTML course from first document to professional application patterns.