Skip to the solution
EasyPhone Screen6 min read

What is JSX?

705 views

The problem

Question presented to candidate: "What is JSX, and what actually happens to it before the browser runs your code?"

What a strong answer should cover:

  • JSX is an XML-like syntax extension to JavaScript, not HTML and not a template language.
  • Browsers cannot run it β€” a compiler (Babel, SWC, oxc, TypeScript) turns it into plain function calls at build time.
  • Under the modern automatic runtime it compiles to jsx() / jsxs() auto-imported from react/jsx-runtime; the older classic runtime compiled to React.createElement and required React to be in scope.
  • The calls return elements β€” plain objects β€” so JSX produces data, it does not render anything.
  • Capitalisation matters: lowercase names compile to strings (host elements), capitalised ones to the identifier itself.
  • It is an expression, so it can be assigned to variables, returned, and put in arrays.
  • Attribute differences: className, htmlFor, camelCased event handlers, style as an object.
  • Security: interpolated values are escaped, which is why dangerouslySetInnerHTML has to exist and is named that way.
  • Depth signal: key is compiled to a separate argument, not into props β€” which is why props.key does not exist.

Clarifying questions expected:

  • "Do you want the syntax rules, or what it compiles to?"

Code / implementation expected: Optional, but showing the compiled output next to the source is the strongest version of this answer.

  • jsx
  • syntax
  • transpilation
  • react basics

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 9 sections

Try it in the playground

Open any example in the playground to run and change it.

ReactJSX is an expression, values are escaped, and 0 is not falsy enough
import { useState } from "react";

export default function App() {
  const [items, setItems] = useState([]);
  const evil = '<img src=x onerror="alert(1)">';

  // JSX is an EXPRESSION: assign it, store it in an array, return it early.
  const badge = <span style={{ background: "#eee", padding: "2px 6px", borderRadius: 4 }}>badge</span>;
  const rows = ["alpha", "beta"].map((t) => <li key={t}>{t}</li>);

  return (
    <div style={{ padding: 24, fontFamily: "system-ui", lineHeight: 1.6 }}>
      <h3>Interpolated values are escaped</h3>
      {/* Renders as literal text β€” no <img> element is created, so the
          onerror handler can never fire. */}
      <p style={{ fontFamily: "ui-monospace, monospace", fontSize: 13 }}>{evil}</p>

      <h3>JSX is just a value</h3>
      <p>Here is a {badge} stored in a variable.</p>
      <ul>{rows}</ul>

      <h3>The classic 0 bug</h3>
      <p>
        {/* ❌ On an empty array this renders a stray "0" */}
        Wrong: [{items.length && <strong>has items</strong>}]
      </p>
      <p>
        {/* βœ… Force a real boolean */}
        Right: [{items.length > 0 && <strong>has items</strong>}]
      </p>
      <button onClick={() => setItems(items.length ? [] : ["one"])}>
        Toggle items (currently {items.length})
      </button>

      <p style={{ color: "#666", fontSize: 13 }}>
        With the list empty, the first line shows a 0 and the second shows
        nothing. React renders 0 as text; false, null and undefined render nothing.
      </p>
    </div>
  );
}

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 13 of 119 in React.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All React.js questions
Up nextEasy

What is the difference between element and component in React?

Continue