Embed expressions and fragments
Put JavaScript values inside JSX
Use braces inside JSX when the value must come from JavaScript rather than fixed text. Expressions produce values: a variable, calculation, property lookup or function call can supply content or a prop. Statements such as a variable declaration or an if statement belong in the surrounding JavaScript, not directly inside a JSX expression slot.
export default function App() { const title = 'Weekend reading'; const available = 4; return ( <section title={title}> <h1>{title}</h1> <p>{available * 2} reading slots</p> {/* This note is only for developers. */} </section> );}The heading is Weekend reading and the paragraph is 8 reading slots. The section title attribute also receives Weekend reading. Quoting title="title" would pass the word title, while title={title} reads the variable. The comment inside braces is ignored in the rendered page; ordinary JSX text is not a JavaScript comment.
Group siblings with a fragment
export default function App() { const remaining = 0; const notice = '<em>Finished</em>'; return ( <> <h1>Reading log</h1> <p>Remaining: {remaining}</p> <p>{false}{null}{undefined}All done</p> <p>{notice}</p> </> );}The fragment groups a heading and three paragraphs without adding a DOM wrapper. In the existing app root, those four elements are siblings. The expected paragraphs are Remaining: 0, All done, and the literal text <em>Finished</em>. Boolean, null and undefined children do not produce text; zero does. A string supplied as a child is escaped as text rather than interpreted as markup. This does not make all arbitrary URLs or HTML insertion methods safe.
A fragment is helpful when an extra div would distort the document structure or layout. The shorthand <>...</> cannot receive a key; an explicitly imported Fragment supports a key when rendering keyed groups, which is covered later. Use a real semantic element if the content needs a landmark or a CSS layout container.
Choose a visible value with an expression
export default function App() { const count = 0; return <p>{count > 0 ? 'Books ready' : 'No books ready'}</p>;}The conditional expression checks count and chooses one string. With zero, the paragraph says No books ready. Change count to 2 and it says Books ready. Do not assume every falsy value disappears: 0 && <p>Ready</p> evaluates to zero, which can be displayed. Detailed conditional rendering comes in Chapter 7.
Common mistakes
Do not render plain objects as children: { { title: "Books" } } supplies an object instead of displayable text. Read its property instead. Objects are valid for some props, such as style, but that is a separate contract. Do not put an expression inside quoted attribute text expecting interpolation. Put the expression immediately after the equals sign.
Practice
Construct a fragment with a heading and two paragraphs, predict its DOM structure and distinguish the displayed number from an empty child. Use the staged browser checklist only in an approved project.