Part 01 / Topic 4 of 8
JSX vs HTML
JSX (JavaScript XML) is the HTML-like syntax you write inside a .jsx file's return (...). It looks like HTML, but it is JavaScript, and a handful of rules follow directly from that:
| Rule | HTML | JSX | Why |
|---|---|---|---|
| Attribute for CSS classes | class="badge" |
className="badge" |
class is a reserved word in JavaScript |
| Attribute casing | onclick, tabindex |
onClick, tabIndex |
JSX attributes are JavaScript object keys, and JS convention is camelCase |
| Inline styles | style="color: red" (a string) |
style={{ color: "red" }} (an object) |
style becomes a real JS object, not a CSS string |
| One root node | <div> and a sibling <p> at the top level are fine |
a component must return exactly one root element (or a fragment, <>...</>) |
a function can only return one value, and JSX compiles to one function call (section 5) |
| Comments | <!-- ... --> |
{/* ... */} |
comments are JavaScript expressions, not HTML syntax |
| Self-closing tags | <img>, <br> are valid without a closing slash |
every tag must self-close or have a matching closer: <img />, <br /> |
JSX is parsed as XML-like, which has no "void element" exception |
| Values | plain text only | {anyJsExpression} |
anything inside {} is evaluated as JavaScript and inserted as text (and automatically escaped - section 5) |
Real-world example: a badge you might write for an order-tracking screen - broken, then fixed, both run for real:
// Broken: `class` instead of `className`.
function OrderBadge({ status }) {
return (
<div class="badge">
<span>{status}</span>
</div>
);
}
Rendering this does not crash - React is forgiving here - but the real browser console shows:
Invalid DOM property `class`. Did you mean `className`?
That warning was captured by actually rendering OrderBadge with React 19.3.1 under Testing Library. Fixed:
function OrderBadge({ status }) {
return (
<div className="badge">
<span>{status}</span>
</div>
);
}
No warning, and the class attribute now actually reaches the DOM.
Done here? The practice set for Part 1 — including the solutions — is on the part page.