What Is JSX?
JSX (JavaScript XML) is a syntax extension for JavaScript that lets you write markup-like code inside a .jsx or .tsx file. Browsers can’t run it. A compiler (Babel, TypeScript, SWC) turns each tag into a function call that returns a plain object describing what to render. JSX is not HTML and it’s not a string template.
1. What JSX compiles to
const el = <h1 className="title">Hello, {name}</h1>;
With the modern JSX transform (React 17 and later):
import { jsxs as _jsxs } from 'react/jsx-runtime';
const el = _jsxs('h1', { className: 'title', children: ['Hello, ', name] });
With the classic transform:
const el = React.createElement('h1', { className: 'title' }, 'Hello, ', name);
This is why older files needed import React from 'react' even when React wasn’t used directly, and newer ones don’t.
2. What you get back
Either way, the result is a React element: a small, immutable object.
{ type: 'h1', props: { className: 'title', children: ['Hello, ', 'Sam'] }, key: null }
React compares these objects between renders to decide what to change in the DOM. See virtual DOM.
3. The rules
- One root. Return a single element, or wrap siblings in a Fragment:
<>...</>. - Close every tag.
<img />,<br />,<input />. - camelCase attributes.
className,htmlFor,onClick,tabIndex.aria-*anddata-*keep their dashes. styletakes an object.style={{ marginTop: 8 }}.- Curly braces hold expressions, not statements.
{user.name},{items.length},{isOpen ? 'Yes' : 'No'}. Noiforforinside braces. - Capitalise components.
<button>is the DOM element;<Button>is your component.
4. Conditionals and lists
function Inbox({ messages, user }) {
return (
<section>
{user ? <p>Welcome, {user.name}</p> : <LoginButton />}
{messages.length > 0 && <Badge count={messages.length} />}
<ul>
{messages.map((m) => (
<li key={m.id}>{m.subject}</li>
))}
</ul>
</section>
);
}
Two things interviewers check:
keymust be stable and unique among siblings, so React can match items between renders. Array indexes break when the list is reordered or filtered.{count && <Badge />}renders0whencountis0, because0is a valid thing to render. Writecount > 0 &&instead.
false, null, undefined and true render nothing.
5. JSX and XSS
Values in braces are escaped before they reach the DOM:
const comment = '<img src=x onerror=alert(1)>';
<p>{comment}</p> // shown as text, not executed
The escape hatch is dangerouslySetInnerHTML, which inserts raw HTML. Only use it with sanitised content. URLs are a second risk: href={userInput} can carry a javascript: URL, so validate schemes. More in XSS and CORS.
6. JSX outside React
JSX is a syntax, not a React feature. Preact, Solid and Vue can all use it with their own compile target. TypeScript supports it in .tsx files and type-checks props.
7. Do you need JSX to use React?
No. You can call createElement directly. Almost nobody does, because nested calls are hard to read.
Quick reference
| HTML | JSX |
|---|---|
class="btn" |
className="btn" |
for="email" |
htmlFor="email" |
onclick="save()" |
onClick={save} |
style="margin-top: 8px" |
style={{ marginTop: 8 }} |
<br> |
<br /> |
<!-- comment --> |
{/* comment */} |
Follow-up questions to expect
- “Is JSX HTML?” No. It compiles to JavaScript function calls and follows JavaScript rules.
- “Why
className?”classis a reserved word in JavaScript, and JSX props map to DOM property names. - “What’s a Fragment for?” Grouping children without adding a wrapper element to the DOM.
