# JSX & Compilation

> Browsers don't understand JSX. See how it compiles down to plain JavaScript.

Subject: React | Lesson 2 of 48 | 3 min read
Interactive lesson: https://codewithanimation.in/react/jsx-and-compilation
Previous: [Declarative vs. Imperative UI](https://codewithanimation.in/react/declarative-ui.md) | Next: [Elements vs. Components](https://codewithanimation.in/react/elements-vs-components.md)

## What it is

JSX is a syntax extension for JavaScript. It allows you to write HTML-like markup directly inside your JavaScript files.

However, browsers have no idea how to read JSX. Before your code can run in the browser, a compiler (like Babel, SWC, or Turbopack) transforms every JSX tag into a plain JavaScript function call—specifically, `React.createElement()` or the modern `_jsx()` transform.

## Why it matters

Instead of artificially separating logic and markup into separate `.js` and `.html` files, React puts them together in components. This keeps the rendering logic (events, state changes) tightly coupled with the UI it controls.

Because JSX ultimately compiles into JavaScript expressions, you can harness the full power of JavaScript inside your UI. You can assign JSX to variables, pass it as arguments, or return it from functions.

## How it works

Whenever you write `<h1 className="title">Hello</h1>`, the compiler rewrites it into a JavaScript object definition.

Because it becomes a JavaScript object, HTML attributes must follow JavaScript naming conventions. This is why you must use `className` instead of `class` (which is a reserved keyword in JavaScript), and `onClick` instead of `onclick`.

## Common mistake: The adjacent element trap

If you try to return two sibling elements (like two `<p>` tags) without wrapping them in a single parent element, React will throw a syntax error. Because JSX compiles to function returns, and a JavaScript function cannot return two distinct values at the same time, you must wrap adjacent elements in a React Fragment: `<> ... </>`.

## Tip: JSX is just an expression

Stop thinking of JSX as HTML. Think of it as a fancy syntax for creating a JavaScript object. This mental shift makes it obvious why you can use `if` statements and `map()` loops around your UI tags.

## Try it: The Compiler's View

What you write is on the left. What the browser actually receives is on the right. Notice how the JSX compiles down into a plain JavaScript object.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/jsx-and-compilation

## Check yourself

### Question 1

What happens to JSX before it runs in the browser?

- A. The browser's HTML parser reads it directly.
- B. It is converted into a WebAssembly binary.
- C. A compiler transforms it into standard JavaScript function calls.

Correct answer: C

Tools like Babel or SWC compile JSX down into standard JavaScript objects so the browser's JavaScript engine can execute it.

- Why not A: Browsers will throw a SyntaxError if they encounter raw JSX.
- Why not B: JSX is converted into standard JavaScript, not WebAssembly.

### Question 2

Why must adjacent JSX elements be wrapped in a single parent tag or Fragment?

- A. Because a JavaScript function can only return one value at a time.
- B. Because React's Virtual DOM only supports a single root node.
- C. To improve CSS styling performance.

Correct answer: A

Under the hood, returning JSX is returning an object. Just as you can't write `return 1, 2;` in JS, you cannot return two adjacent JSX tags without grouping them.

- Why not B: The Virtual DOM handles arrays of nodes fine. The issue is strictly about JavaScript syntax returning values.
- Why not C: It has nothing to do with CSS. In fact, a Fragment `<></>` doesn't even render an HTML node.

### Question 3

Why does JSX use `className` instead of the standard HTML `class` attribute?

- A. Because `class` is a reserved keyword in JavaScript.
- B. To differentiate React elements from regular HTML elements.
- C. Because React uses a proprietary CSS engine.

Correct answer: A

Because JSX is just JavaScript, and `class` is a reserved word used to define JavaScript classes, React opted for the DOM property name `className`.

- Why not B: It's purely a technical limitation of the JavaScript language.
- Why not C: React just sets the standard DOM `className` property behind the scenes.

## Remember this

- JSX is not HTML; it is a syntax extension for JavaScript.
- Compilers transform JSX into plain JavaScript objects (`React.createElement`).
- You must wrap adjacent elements in a parent tag or `<>` Fragment.
- Use camelCase for all attributes (e.g., `className`, `onClick`).
