LearnThatStack Ace your next interview
React · question
Question 2 of 103

What is JSX and why is it used in React?

beginner
← All React questions
Re-explain

JSX, short for JavaScript XML, is a syntax extension that lets you write HTML-like markup inside JavaScript. It makes components easier to read and write, and lets you mix JavaScript expressions into markup. JSX also gives better error messages and enables static type checking.

A compiler such as Babel turns JSX into function calls. The classic transform produces React.createElement() calls.

// JSX
const element = <h1>Hello, World!</h1>;

// Transpiled to
const element = React.createElement('h1', null, 'Hello, World!');

The new JSX transform, supported since React 17, calls react/jsx-runtime instead, so you can use JSX without importing React. React 19 requires it.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for React One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • What does a JSX expression evaluate to at runtime? A plain object, a React element, that describes type, props and children. It is not a DOM node. React reads it later to update the DOM.
  • Why can a component return only one root element from JSX, and how do you work around it? Each tag compiles to one function call, so you return one expression. Wrap siblings in a Fragment (<>...</>) instead of an extra div.
  • You can't put an if statement inside JSX. How do you render something conditionally? Only expressions fit in curly braces, so use a ternary, &&, or a variable set before return. With &&, a count of 0 renders 0.
  • How does JSX help protect against XSS, and how can you lose that protection? React DOM escapes strings embedded in JSX before rendering. dangerouslySetInnerHTML skips that, so sanitize anything you pass to it.
  • Could you write React without JSX? When would that make sense? Yes, by calling createElement directly. It can fit a page with no build step or a tiny widget, but nested trees get hard to read.

What you can say

  1. JSX is a syntax extension for JavaScript that lets you write HTML-like markup right inside your components.
  2. We use it because a tree of tags is much easier to read than nested function calls, and you can drop JavaScript expressions into the markup with curly braces.
  3. A compiler like Babel turns each tag into a function call, so with the classic transform an h1 becomes React.createElement('h1', null, 'Hello').
  4. Since React 17 the new JSX transform calls react/jsx-runtime instead, so you don't have to import React in every file, and React 19 requires it.
  5. Because JSX compiles to plain function calls, React can give better error messages and tools can type-check your markup.
  6. The trade-off is a build step, because browsers can't run JSX, though it's optional and you could call createElement by hand if you had no build tooling.

Weak answers to avoid

  • Calls JSX HTML inside JavaScript JSX is JavaScript syntax that compiles to function calls, which is why you write className and htmlFor. Say what it compiles to.
  • Thinks the browser or React parses JSX The browser never sees JSX. A compiler such as Babel removes it at build time. Say where the transform happens.
  • Says you must always import React to use JSX That only holds for the classic transform. The new transform, available since React 17 and required in React 19, uses react/jsx-runtime.
  • Lists benefits but never shows the compiled output The interviewer wants proof you know what JSX becomes. One line of JSX next to its createElement call shows it.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the React cheatsheet.

← Back to all React questions
Pro · $10/mo

88 of 103 React answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime