JSX

Also known as: JavaScript XML

On this page 7

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 constructExampleCompiled meaning
Lowercase tag<div>String type, rendered as a host element
Capitalized tag<Card>Reference to a component variable
Attributetitle="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
JSX constructs and their compiled form

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 formJSX formReason
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
HTML habits and their JSX equivalents

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.

ApproachTypical usersLogic modelMain trade-off
JSXReact, Preact, SolidFull JavaScript expressionsFlexible, but mixes concerns visually
HTML templates with directivesVue.js, AngularRestricted directive syntaxEasier static analysis, less expressive
Svelte markupSvelte, SvelteKitBlocks such as {#each}Compiles away, needs its own tooling
Tagged template literalsLit, htmStandard JavaScript stringsNo build step, weaker type checking
Direct function callsLibraries without a compilerPlain h() callsNo tooling, verbose for deep trees
Ways to describe component output

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.

SymptomCauseFix
Adjacent elements errorSeveral roots returnedWrap them in a fragment
Stray 0 on the pagecount && <List /> with zeroUse count > 0 && or a ternary
List state jumps between itemsArray index used as keyUse a stable unique ID
Component not renderedLowercase component nameCapitalize the identifier
Handler fires on every renderonClick={run()} calls itPass onClick={run} instead
Frequent JSX errors and how to resolve them

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.