Consider this Astro component code snippet:
---
const isLoggedIn = true;
---
{isLoggedIn ? Welcome back!
: Please log in.
}What will be rendered in the browser?
---
const isLoggedIn = true;
---
{isLoggedIn ? <p>Welcome back!</p> : <p>Please log in.</p>}Check the value of isLoggedIn and how the ternary operator works.
The variable isLoggedIn is true, so the expression renders the first part of the ternary: <p>Welcome back!</p>.
Choose the option that correctly uses a conditional expression to render a message based on showMessage:
--- const showMessage = false; ---
Remember that Astro uses JavaScript expressions inside curly braces, and if statements are not expressions.
Option D uses a logical AND expression which is valid JSX/TSX style syntax inside Astro. Options A, B, and C have syntax errors.
Given this Astro component snippet:
---
const user = { name: 'Alex', isAdmin: false };
---
{
user.isAdmin ? Admin: {user.name}
: user.name ? User: {user.name}
: Guest
}What will be rendered?
---
const user = { name: 'Alex', isAdmin: false };
---
{
user.isAdmin ? <p>Admin: {user.name}</p> : user.name ? <p>User: {user.name}</p> : <p>Guest</p>
}Check the values of user.isAdmin and user.name carefully.
user.isAdmin is false, so the first condition fails. Then user.name is truthy ('Alex'), so the second condition renders <p>User: Alex</p>.
Look at this Astro component code:
---
const isActive = true
---
{isActive ? Active
: Inactive
}Why does it cause a syntax error?
---
const isActive = true
---
{isActive ? <p>Active</p> : <p>Inactive</p>}Check the frontmatter block syntax carefully.
The frontmatter block must be closed with ---. Here it is missing, causing a syntax error.
Which statement best describes how conditional rendering works differently in Astro server components versus client components?
Think about when server components generate HTML versus when client components run JavaScript.
Astro server components generate HTML during build or server rendering, so conditionals run then. Client components run in the browser, so their conditionals happen at runtime.
