What is JSX and how does it work under the hood?
Hint
JSX is syntactic sugar — Babel transforms it into React.createElement() calls
Full Answer
JSX is a syntax extension that looks like HTML inside JavaScript. It is NOT valid JS — it gets compiled by Babel into React.createElement() calls.
// What you write
const el = <h2 className="title">Hello {name}</h2>;
// What Babel compiles it to
const el = React.createElement('h1', { className: 'title' }, 'Hello ', name);
// React 17+ — new JSX transform (no need to import React)
import { jsx as _jsx } from 'react/jsx-runtime';
const el = _jsx('h1', { className: 'title', children: ['Hello ', name] });
Key rules:
- Single root element required (use
<></>Fragment if needed) - Use
classNamenotclass,htmlFornotfor - Expressions in
{}— not statements (if,for) - Self-close empty tags:
<img />,<br /> - camelCase for attributes:
onClick,onChange
💡 JSX returns plain JS objects (React elements). They're just descriptions — cheap to create. React uses these to build and diff the Virtual DOM.
More React Fundamentals Questions
Practice this in a timed sprint
5 free questions, no signup required