What is JSX in React?

BeginnerReact interview questionBy , updated 17 November 2026

Short answer

JSX is a syntax extension for JavaScript that lets you write HTML-like markup inside your code. Browsers cannot run it, so a compiler such as Babel, SWC or esbuild turns each tag into a function call, jsx("h1", { children: "Hi" }), that returns a plain object called a React element. JSX is optional, but it makes the shape of the UI easy to read.

What JSX compiles to

Since React 17, the default is the automatic JSX runtime. The compiler adds an import from react/jsx-runtime and replaces each tag with a call to jsx (one child) or jsxs (a static list of children). You no longer need import React from "react" at the top of every file.

// What you write
function Greeting({ name }) {
  return (
    <h1 className="title">
      Hello, {name}
    </h1>
  );
}
// Roughly what the compiler outputs
import { jsxs as _jsxs } from "react/jsx-runtime";

function Greeting({ name }) {
  return _jsxs("h1", { className: "title", children: ["Hello, ", name] });
}

Before React 17, the classic runtime compiled the same tag to React.createElement("h1", { className: "title" }, "Hello, ", name), which is why old code imports React everywhere. Both produce the same thing: an object with a type ("h1" or a component function), props and a key. React reads these objects to work out what the DOM should look like. They are not DOM nodes, and creating one is cheap. In development, the compiler uses jsxDEV from react/jsx-dev-runtime, which adds source locations for better warnings.

A lowercase tag such as <div> becomes the string "div", a built-in element. A capitalised tag such as <Greeting> becomes a reference to the Greeting variable, so components must start with a capital letter. The difference between these objects and the functions that return them is covered in React element vs component vs node.

Expressions in braces

Curly braces drop back into JavaScript. Anything that is an expression works: variables, function calls, ternaries, map, template literals.

<ul className={isOpen ? "menu open" : "menu"}>
  {items.map((item) => (
    <li key={item.id}>{item.label.toUpperCase()}</li>
  ))}
</ul>

Statements do not work inside braces. You cannot write if, for or const there, because JSX attributes and children must be values. Put the logic above the return, or use an expression form such as a ternary. Because JSX is itself an expression, you can store it in a variable, pass it as a prop or return it from a function.

What renders depends on the value. Strings and numbers render as text. null, undefined, true and false render nothing, which is what makes {isAdmin && <Panel />} work. Arrays render each item. Plain objects throw an error, so {user} fails where {user.name} works.

Attributes that differ from HTML

JSX attributes become JavaScript object keys, so a few names follow the DOM property names, not the HTML attribute names.

HTML JSX Why
class className class is a reserved word in JavaScript
for htmlFor for is a reserved word
onclick="..." onClick={handler} Events are camelCase and take a function, not a string
style="color: red" style={{ color: "red" }} Style is an object with camelCase keys
tabindex tabIndex Most multi-word attributes are camelCase
<input> <input /> Every tag must be closed

data-* and aria-* attributes keep their hyphens. Comments are written as {/* comment */}.

Why JSX escapes strings

When you write {comment.text}, React sets it as a text node, not as HTML. If the text is <img src=x onerror=alert(1)>, the user sees those characters on screen, and no script runs. This is the main reason JSX is safer than building HTML strings and assigning them to innerHTML: user input is escaped by default.

The escape hatch is dangerouslySetInnerHTML={{ __html: html }}, and the name is a warning. Only pass HTML you have sanitised. Escaping also does not cover URLs: <a href={userUrl}> with a javascript: URL is a separate risk, so validate URLs yourself. See What is XSS and how do you prevent it? for the full picture.

One root, or a fragment

A JSX expression has a single root, because it compiles to a single function call. To return siblings without adding a wrapper div, use a fragment:

function Columns() {
  return (
    <>
      <td>Name</td>
      <td>Price</td>
    </>
  );
}

<>...</> compiles to jsx(Fragment, { children: [...] }). Use <Fragment key={id}> when the fragment is inside a list. What are React fragments used for? covers the details.

Practise it

Parse a JSX-like string into a virtual DOM tree and render it back to escaped HTML makes you do the compiler's and renderer's job by hand. Implement a minimal React-like renderer with virtual DOM diffing shows what React does with the element objects, and Escape HTML and sanitize URLs to close XSS holes covers the escaping JSX gives you for free.

Common follow-up questions

Is JSX HTML?

No. It looks like HTML but is JavaScript. It compiles to function calls, uses DOM property names such as className, and is stricter: every tag must close and attributes take JavaScript values.

Can you use React without JSX?

Yes. Call createElement("h1", { className: "title" }, "Hello") from react directly. It works, but nested trees get hard to read, which is why almost everyone uses JSX.

Where does the key go after compilation?

With the automatic runtime, key is passed as a separate argument: jsx("li", { children: label }, item.id). It is not part of props, so a component cannot read props.key. In React 19, ref is an ordinary prop for function components.

Does TypeScript understand JSX?

Yes. Files must use the .tsx extension, and the jsx option in tsconfig.json (usually "react-jsx" or "preserve") controls whether TypeScript compiles it or leaves it to the bundler.

Mistakes to avoid

  • Writing class or for instead of className and htmlFor

  • Putting an if statement inside braces, or rendering a plain object

  • Naming a component in lowercase, so React treats it as an unknown HTML tag

  • Using dangerouslySetInnerHTML with unsanitised user content

  • Wrapping every return in a div when a fragment would keep the DOM clean

Practise it in code

Interviewers rarely stop at the definition. These questions open in an editor with tests.

  1. Parse a JSX-like string into a virtual DOM tree and render it back to escaped HTMLHard50 min
  2. Implement a minimal React-like renderer with virtual DOM diffing and keyed reconciliationHard75 minFree
  3. Escape HTML and sanitize URLs to close XSS holesMedium30 min