JSX (JavaScript XML) is a syntax extension for JavaScript that lets developers write user interface structure with HTML-like tags directly inside JavaScript code, which a compiler then turns into ordinary function calls. In web applications built with React and several related libraries, JSX is the most common way to describe component output declaratively, combining markup, logic and data in one readable expression.
JSX is not valid JavaScript on its own and is not a template language interpreted at run time. Browsers never see it: a build tool such as Babel, TypeScript, esbuild or SWC transforms every tag before the code ships, so JSX is a developer convenience with zero inherent runtime cost beyond the calls it produces.
How JSX compiles into JavaScript
JSX works by compile-time transformation: each element becomes a call that creates a plain object describing a node, its properties and its children. The element <Button color="blue">Save</Button> becomes a call such as jsx(Button, { color: "blue", children: "Save" }), and the library later uses those objects to build or update the real document.
Two transform modes exist. The classic runtime calls React.createElement and requires the library to be in scope in every file. The automatic runtime, introduced with React 17, imports helper functions from react/jsx-runtime automatically, removes the scope requirement and produces slightly smaller output. The pragma setting decides which factory function is called, which is how Preact, Solid and other libraries reuse the same syntax.
The table below traces how common JSX constructs map onto the compiled output.
| JSX construct | Example | Compiled meaning |
|---|---|---|
| Lowercase tag | <div> | String type, rendered as a host element |
| Capitalized tag | <Card> | Reference to a component variable |
| Attribute | title="Hi" | Property in the props object |
| Expression container | {user.name} | Any JavaScript expression value |
| Spread attributes | {...props} | Object spread into props |
| Fragment | <>...</> | Grouping without an extra wrapper node |
| Nested children | <ul><li/></ul> | Children passed as props or arguments |
Capitalization matters: the compiler treats lowercase names as built-in elements and capitalized names as component references. Writing <card> instead of <Card> silently renders an unknown HTML element rather than the component.
Syntax rules that differ from HTML
JSX looks like HTML but follows JavaScript and DOM property conventions, so several familiar habits produce errors. Every expression must return a single root element, every tag must be explicitly closed, and attribute names follow the camelCase DOM API rather than HTML attribute spelling.
The following comparison lists the differences that most often trip up developers coming from plain HTML.
| HTML form | JSX form | Reason |
|---|---|---|
class="box" | className="box" | class is a reserved word |
for="email" | htmlFor="email" | for is a reserved word |
onclick="run()" | onClick={run} | Handlers are function references |
style="color:red" | style={{ color: "red" }} | Styles are JavaScript objects |
<br> | <br /> | All tags must be closed |
<!-- note --> | {/* note */} | Comments are JavaScript comments |
Control flow is expressed with plain JavaScript expressions rather than special directives. Conditional output uses the ternary operator or logical AND, and lists use array.map() with a stable `key` prop on each item so that the reconciler can match elements between renders. Statements such as if or for cannot appear directly inside braces, because braces accept expressions only.
JSX compared with other templating approaches
JSX represents a JavaScript-first philosophy: markup lives inside code, whereas template languages place limited logic inside markup. Vue.js, Angular and Svelte use HTML-based templates with directives, although Vue.js can also accept JSX through a plugin.
| Approach | Typical users | Logic model | Main trade-off |
|---|---|---|---|
| JSX | React, Preact, Solid | Full JavaScript expressions | Flexible, but mixes concerns visually |
| HTML templates with directives | Vue.js, Angular | Restricted directive syntax | Easier static analysis, less expressive |
| Svelte markup | Svelte, SvelteKit | Blocks such as {#each} | Compiles away, needs its own tooling |
| Tagged template literals | Lit, htm | Standard JavaScript strings | No build step, weaker type checking |
| Direct function calls | Libraries without a compiler | Plain h() calls | No tooling, verbose for deep trees |
A key distinction is that JSX itself is independent of the Virtual DOM. React uses the objects for virtual DOM reconciliation, while Solid compiles the same syntax into fine-grained reactive DOM updates with no virtual tree at all. JSX defines the notation, and the library defines the rendering semantics.
Tooling, types and security
In practice JSX is processed by the same toolchain that bundles the application. Files typically use the `.jsx` or `.tsx` extension, and TypeScript checks JSX elements against component prop types, catching missing or misspelled props at compile time. Linters add rules for hook usage, missing keys and accessibility attributes.
JSX also provides a default defense against cross-site scripting. Strings placed in expression containers are escaped automatically before insertion, so user input renders as text rather than markup. The deliberately awkward escape hatch dangerouslySetInnerHTML bypasses this protection and should only receive sanitized, trusted HTML. Escaping does not protect href values, so URLs from users still need validation against schemes such as javascript:.
Frameworks such as Next.js, Remix and Astro use JSX for both server and client code. With React Server Components, the same JSX can render on the server and stream serialized output to the browser, which shows that JSX describes structure, not where or when rendering happens.
Common mistakes and limitations
Most JSX problems come from forgetting that it is JavaScript, not HTML. The table below maps frequent symptoms to their causes and fixes.
| Symptom | Cause | Fix |
|---|---|---|
| Adjacent elements error | Several roots returned | Wrap them in a fragment |
Stray 0 on the page | count && <List /> with zero | Use count > 0 && or a ternary |
| List state jumps between items | Array index used as key | Use a stable unique ID |
| Component not rendered | Lowercase component name | Capitalize the identifier |
| Handler fires on every render | onClick={run()} calls it | Pass onClick={run} instead |
JSX has genuine limitations. It requires a build step, which adds configuration in small projects. Large components with deeply nested conditionals become hard to read, a sign that logic should move into smaller components or helper functions. Debates about mixing markup and logic are partly about taste, but practitioners broadly agree that component boundaries, not file types, are the meaningful unit of separation.
Stripped to the essentials
JSX lets programmers write what a screen should look like using tags that resemble ordinary web page code, but inside a JavaScript file. Before the app reaches the browser, a tool rewrites those tags into regular JavaScript instructions.
Think of it as shorthand that gets expanded: like writing a recipe with familiar symbols that a kitchen assistant translates into exact steps. The result is code that is easier to read and harder to get wrong, while the browser still receives only standard JavaScript.
Where it is applied
A team building an order dashboard for a regional retailer writes its interface in React with TypeScript. The order table component uses JSX to map an array of about 200 orders into rows, each with a stable order ID as its key, and a ternary shows a loading skeleton until data arrives.
When a developer mistypes the prop status as staus, the TypeScript compiler flags the error before the code is committed. Customer names containing angle brackets render safely as text because JSX escapes them. The build pipeline compiles every .tsx file with the automatic runtime, and the shipped bundle contains only plain JavaScript function calls, with no trace of the original tags.