Skip to content

JSX Precisely: The Rules Behind the Markup

A precise look at JSX – what the compiler turns it into, why curly braces hold expressions rather than statements, how attributes, children, conditions and lists work, and where beginners stumble.

≈ 10 min readSoftwareFrontendReact

JSX Precisely: The Rules Behind the Markup

JSX isn’t a template language and it isn’t an HTML variant that somehow tolerates JavaScript. It’s a notation for function calls: every element you write is translated into a call before execution, and that call returns an ordinary JavaScript object.

Editorial note (July 2026): The main text explains the classic JSX transform, which was the published state in June 2020. Since React 17, the automatic transform can use functions from react/jsx-runtime; that doesn’t change the mental model. How React later reconciles the resulting elements is covered in the article published afterward, How React Renders and Updates the Tree.

The React overview had a single sentence on this – looks like HTML, but is JavaScript. The sentence is true and still prevents none of the mistakes that happen in everyday work: reserved attribute names, statements in curly braces, a lonely 0 in the UI, keys from array indexes. So here are the rules in order, each with a tiny example and, where it matters, with the mistake you make if you don’t know the rule.

What the compiler really turns it into#

JSX isn’t a JavaScript language feature. No browser understands it. Before the code runs, a build step like Babel translates every JSX element into a normal function call. This one fact explains almost all the rules that follow.

As of June 2020, the target of this translation is React.createElement. The markup becomes a call, and the call becomes a plain object.

const el = <button className="primary" onClick={handleClick}>Save</button>;

becomes, roughly:

const el = React.createElement(
  "button",
  { className: "primary", onClick: handleClick },
  "Save",
);

The returned object only describes what should be rendered – it’s not a DOM node, nothing happens in the browser yet. React later takes this object and creates or updates the real DOM from it. Such a description object is called a React element. With that in mind, it becomes clear why a typo in JSX often shows up as a perfectly normal JavaScript error: in the end, it is a function call.

Expression, not statement#

The most important rule for the content of curly braces is: between { and } there’s an expression, not a statement. An expression is something that produces a value – user.name, a + b, price > 0 ? "ok" : "no", a function call. A statement is something that does something but doesn’t yield a value – if, for, while, switch. Statements aren’t allowed inside the braces.

// works – an expression
<p>{user ? user.name : "Guest"}</p>

// does not work – if is a statement
<p>{if (user) { user.name }}</p>

That’s why in JSX you reach so often for the ternary operator ? : and the logical &&: they’re the expression variants of if. If you need a real branch with several arms, you pull it in front of the return and work there with a perfectly normal if:

function Status({ status }) {
  let text;
  if (status === "open") text = "Open";
  else if (status === "paid") text = "Paid";
  else text = "Unknown";

  return <span>{text}</span>;
}

That isn’t a detour. It’s the clean separation: logic at the top, presentation at the bottom. A common beginner reflex is to nest ever more complex ternaries until nobody reads them anymore. As soon as you reach two levels, the decision belongs at the top.

Attributes are props, and they’re named differently than in HTML#

JSX attributes look like HTML attributes but are the properties of the created object – that is, props. Because the target language is JavaScript and not HTML, they follow JavaScript’s rules, and in a few places that visibly differs from HTML.

Two names are reserved, because class and for are keywords in JavaScript. That’s why the class attribute is called className and the label attribute htmlFor:

<label htmlFor="email" className="field-label">Email</label>
<input id="email" />

All multi-word HTML attributes are written in camelCase: tabindex becomes tabIndex, readonly becomes readOnly, maxlength becomes maxLength. Event handlers are called onClick, onChange, onSubmit – with a capital C in the middle – and get a function, not a string:

// correct – a function reference
<button onClick={handleSave}>Save</button>

// wrong – calls handleSave IMMEDIATELY on render and passes the return value
<button onClick={handleSave()}>Save</button>

The second case is one of the most common beginner mistakes. onClick={handleSave()} runs handleSave during rendering and attaches its result to the handler. If you want to pass arguments, you wrap the call in an arrow function: onClick={() => handleSave(id)}.

You give values in curly braces, fixed strings in quotation marks. An attribute without a value is implicitly true:

<input type="text" value={value} disabled />
// disabled is equivalent to disabled={true}

How values in content are rendered – and what disappears#

What React makes of an expression in the content depends on the type of the value, and a whole series of small surprises lurk here. Strings and numbers are output as text. Arrays are rendered element by element. But null, undefined, true and false render nothing – they disappear without a trace. That’s exactly what makes conditional rendering possible in the first place.

<div>
  {message}          {/* string or number -> gets displayed */}
  {null}             {/* nothing */}
  {isAdmin && <Panel/>} {/* Panel only when isAdmin is true */}
</div>

A classic mistake lies in the &&. It renders the right-hand element only if the left side is truthy – but it returns the left side if it’s falsy. With booleans this is harmless, because false disappears. With a number, it isn’t:

// pitfall: if count is 0, a 0 appears in the UI, not nothing
{count && <List />}

// correct: convert to a boolean first
{count > 0 && <List />}

The 0 simply isn’t false but a number, and numbers get rendered. If you’ve ever hunted for a lonely 0 in your UI, you know this mistake. The rule behind it is simple: in front of a &&, it’s best to have a real boolean, not a value that happens to be falsy.

One element, one root node – and fragments#

Because a JSX expression ultimately produces a single object, a component has to return exactly one root element. Two elements side by side without wrapping are a syntax error. You could wrap everything in a <div>, but that creates an additional, often unwanted node in the DOM. That’s what the fragment is for – a wrapper that leaves nothing behind in the DOM:

function Row() {
  return (
    <>
      <td>Name</td>
      <td>Amount</td>
    </>
  );
}

The short form <>...</> is almost always enough. Only when the fragment itself needs a key – say, as a direct child of a list – do you have to use the long form <Fragment key={id}>...</Fragment>, because the short form doesn’t accept attributes.

By the way, the parentheses around multi-line JSX after return aren’t cosmetics against indentation but protection against a JavaScript trap: if the first element is on a new line directly after return, JavaScript automatically inserts a semicolon, and the function returns undefined. The parentheses prevent that.

Lists and keys: the most underestimated rule#

You render a list by using map to turn an array into an array of elements. Each of these elements needs a key attribute. The key isn’t a decorative detail – it’s the information React uses on the next render to recognize which old element corresponds to which new one.

<ul>
  {invoices.map((i) => (
    <li key={i.id}>{i.number}</li>
  ))}
</ul>

The key has to have two properties: it must be unique among its siblings, and it must be stable, that is, always the same for the same data. That’s why you use a domain ID, not a random value and – this is the sore point – preferably not the array index.

The index as a key seems to work as long as the list never changes. But as soon as you insert something at the front, delete or sort, the indexes shift, and React assigns the old states to the wrong elements. This shows up in ghostly bugs: an input field keeps its text even though the row should be a completely different one; a checkbox stays checked even though the corresponding element was deleted. The index describes a position, but the key is supposed to describe an identity – and position and identity are two different things. Only when a list is guaranteed to be static and never re-sorted is the index acceptable as a key. How React performs this comparison in detail when re-rendering is a topic of its own.

Spread, children and the small pitfalls on the side#

You can set props individually or pass them through from an object with the spread operator. That’s convenient but deserves caution:

const props = { type: "text", value: value, onChange: handleChange };
<input {...props} />

It’s clean when you know exactly what’s in the object. It gets messy when you blindly pass through an entire foreign object and thereby unknowingly set attributes you didn’t want at all. If a single attribute follows the spread, the last one wins – <input {...props} value={otherValue} /> overrides the value from the object.

The content between the opening and closing tag ends up in the special prop children. A component that renders children knows nothing about its content and is usable everywhere precisely for that reason. You can also pass children explicitly as an attribute, but that’s rarely nicer than natural nesting.

A few more details that come up again and again: inline styles in JSX are an object, not a CSS string, and their keys are in camelCase – <div style={{ backgroundColor: "red", fontSize: 14 }} />, with double braces, because the outer pair starts the expression and the inner one is the object. You write comments inside JSX as an expression: {/* so */}. And you only insert raw HTML from a string via dangerouslySetInnerHTML – the unwieldy name is intentional, because this is exactly where cross-site scripting holes arise when the string comes from a foreign source. Normal text in JSX is automatically escaped by React and is therefore safe; this one path deliberately bypasses that protection.

Why precision pays off#

All these rules follow from a single idea: JSX is a notation for function calls that produce a description object. The reserved names, the expression rule, the disappearance of null and false, the 0 trap, the requirement for one root node, the meaning of the key – none of it is arbitrary; it all follows from the fact that in the end JavaScript runs, not HTML.

If you read JSX this way, you stop working against the language. You no longer accidentally write className as class, you turn a number in front of && into a real comparison, you assign keys from domain IDs instead of indexes – not because a linter complains, but because you understand what the compiler makes of it. The individual building blocks only touched on here – data flow through props, the question of state ownership, rendering on the server – have their own articles. JSX is the beginning of everything, and that’s exactly why it pays to be precise here.

Further reading#

Revised and translated from German with the help of AI.