JSX syntax and compilation
How JSX's braces and rules actually work, what it compiles down to, and the places it quietly differs from HTML.
Setting up a React projectWhy React, and the component model
JSX syntax and compilation
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- JSX is not a string and not HTML — it's syntax that compiles to plain JavaScript function calls describing an element tree.
- Curly braces {} drop into JSX from JavaScript-land, letting you embed any expression — a variable, a function call, a ternary — directly in markup.
- Every JSX element, under the hood, becomes a call that produces a plain JavaScript object describing a type, its props, and its children; nothing about it is HTML at runtime.
- Modern tooling uses an automatic JSX transform that imports what it needs behind the scenes, but the older explicit React.createElement form compiles to effectively the same calls and is worth recognizing.
- JSX enforces a handful of rules HTML doesn't: attributes are camelCase (className, onClick), every element must close, and a component can only return one root node — use a Fragment (<>...</>) when you don't want an extra wrapper div.
It looks like HTML, but it compiles
The first time you see this, it’s reasonable to wonder what it even is:
function Greeting({ name }) {
return <h1>Hello, {name}!</h1>;
}
That return value is JSX — JavaScript XML. It is not a string (there are no quotes around it), and it is not HTML (the browser has no idea what to do with it). It’s syntax, on top of JavaScript, that a compiler transforms into plain JavaScript before any of it runs.
The compiler most projects use is Babel (Vite wires this up for you automatically), and what it produces, historically, was a call to React.createElement:
function Greeting({ name }) {
return React.createElement("h1", null, "Hello, ", name, "!");
}
React.createElement(type, props, ...children) is an ordinary JavaScript function. It returns a plain object — not a DOM node, just data — describing what should be rendered: the tag name, its props, and its children. That object is called a React element. JSX is purely a more readable way to write calls to this function (or its modern equivalent); it adds zero capability JavaScript didn’t already have.
The modern, automatic transform
Newer tooling (including what Vite’s React template uses today) compiles JSX through what’s called the automatic JSX transform, which produces calls to a different, internal function — commonly aliased as jsx — imported automatically from react/jsx-runtime:
import { jsx as _jsx } from "react/jsx-runtime";
function Greeting({ name }) {
return _jsx("h1", { children: ["Hello, ", name, "!"] });
}
The practical effect is that you no longer need import React from "react" in every file just to use JSX — the compiler inserts the right import for you behind the scenes. Conceptually, though, nothing has changed from the createElement version: JSX still compiles to a function call that returns a plain object describing an element. Knowing both forms exist matters mostly so that older code and tutorials using React.createElement directly don’t look like a different language — it’s the same idea, written by hand instead of compiled.
Dropping into JavaScript with {}
Curly braces are JSX’s escape hatch back into plain JavaScript. Anything between { and } is evaluated as a JavaScript expression, and the result is rendered in that spot:
function PriceTag({ price, onSale }) {
return (
<p>
Price: ${price.toFixed(2)}
{onSale ? " (on sale!)" : ""}
</p>
);
}
{price.toFixed(2)} calls a method and inserts the result. {onSale ? " (on sale!)" : ""} embeds a ternary. The rule to internalize: braces accept an expression — something that produces a value — not a statement. You can write {condition ? a : b} but not {if (condition) { ... }}; if doesn’t produce a value, so it has no place inside {}. (Conditional rendering patterns that work around this are their own lesson, coming up shortly.)
Where JSX quietly differs from HTML
JSX looks enough like HTML that its differences are easy to trip over:
className, notclass.classis a reserved word in JavaScript, so the prop is namedclassNameinstead. Similarly,for(as in a<label>’s target) becomeshtmlFor.- Attributes are camelCase.
onclickbecomesonClick,tabindexbecomestabIndex,maxlengthbecomesmaxLength— following JavaScript’s property-naming convention rather than HTML’s lowercase-everything convention. - Every element must close.
<img>and<br>are legal, unclosed HTML; in JSX they must be self-closing:<img />,<br />. - A component returns exactly one root element. This one isn’t an HTML difference so much as a consequence of JSX compiling to a single function call —
return <h1 /> <p />has two root elements and is a syntax error. Wrap siblings in a single parent, or in a Fragment, written as empty angle brackets:
function Header() {
return (
<>
<h1>Title</h1>
<p>Subtitle</p>
</>
);
}
A Fragment groups elements to satisfy the one-root-element rule without adding any actual node to the DOM — unlike wrapping them in a <div>, which does add one, and can quietly break a CSS layout that depends on a specific parent-child structure (flex/grid children, or table rows).
What the video covers
Codevolution’s episode, from their current React 19 series, walks through JSX itself — the {} expression syntax and the compile step — at a focused, up-to-date pace. It leans lighter on the HTML-vs-JSX naming differences (className, camelCase props, fragments), which the series’ direct follow-up episode (linked below) covers in depth and which this lesson’s own section above fills in either way. Watch the primary video for the compilation mental model; use this lesson’s “quietly differs from HTML” section, or the deeper link, for the rules themselves.
Where this fits
JSX is what you write; the next lesson covers what React actually does with the tree of elements it compiles to — the virtual DOM and reconciliation.
Check yourself
Is JSX valid JavaScript on its own, without any compilation step?
No. JSX is special syntax that a build tool (via Babel or a similar compiler) transforms into plain JavaScript before it ever reaches the browser. The browser never sees JSX directly — only the function calls it compiles down to.
What can you put inside curly braces {} in JSX, and what can't you put there?
Any valid JavaScript expression — a variable, a function call, a ternary, arithmetic, a template string. You cannot put statements there, such as an if block or a for loop, because an expression (something that produces a value) is all a brace slot accepts.
Why does JSX use className instead of class, and onClick instead of onclick?
JSX attributes become properties on JavaScript objects, and class and for are reserved words in JavaScript (class) or a poor match for camelCase convention, so React renamed them to className and htmlFor. Event handlers follow JavaScript's camelCase convention generally, which is why onclick becomes onClick, onchange becomes onChange, and so on.
Why would you reach for a Fragment (<>...</>) instead of just wrapping two elements in a div?
A component can only return one root element, so two sibling elements need something to wrap them. A div adds an extra, often meaningless node to the actual DOM (which can break CSS layouts like flexbox or grid, or table structure). A Fragment satisfies the one-root-element rule without adding any element to the rendered output at all.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.