JSX Under the Microscope: Syntax and Compilation

Learn how JSX, React's syntax extension, mixes HTML with JavaScript. Understand its rules, how to embed expressions, and see how tools like Babel compile it into browser-readable JavaScript.

Lesson ProgressStep 1 of 9
<h1>Hello, World!</h1>
0 EXP

Let's see how React describes UI. We start with something that looks just like HTML...

<h1>Hello, World!</h1>

What is JSX?

JSX stands for JavaScript XML. It's a syntax extension for JavaScript that lets you write HTML-like markup inside a JavaScript file. Instead of separating your logic and markup into different files (like `.js` and `.html`), React lets you combine them in components.

const element = <h1>Hello, world</h1>;

This looks like HTML, but it's actually JavaScript. It gives you the full power of JavaScript within your UI structure.

System Check

What does JSX stand for?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🏆
JSX Syntax Pro

Write a valid JSX component with props and children.

🏗️
Compiler Mind

Correctly identify the output of a JSX compilation.

✍️
JSX Rules Expert

Prove mastery of JSX rules (e.g., className, single root).

Mission: Build a Reusable Component

Create a `UserProfile` component. It should return a `div` with a `className` of "user-card". Inside, it needs an `<h2>` that displays a `name` prop and a `<p>` that displays an `email` prop.

A.D.A. Feedback:

> System integrity looks stable. Code is valid.

Challenge: Order the Compilation Process

Drag the items into the correct order to show how JSX becomes a real element on the page.

React.createElement(App)
<App />
{ type: App, ... }
Babel (Compiler)
Actual DOM Node

Challenge: Fix the JSX Rules

Fill in the blanks to make this component return valid JSX.

function Profile() {return (
<h1>My Profile</h1>
<div="profile-bio">...</div>
)}

Consult A.D.A.

Community Holo-Net

Peer Project Review

Submit your "Reusable Component" project for feedback from other Net-Runners.

JSX in Action: From Syntax to Dynamic UIs

JSX (JavaScript XML) is more than just a convenient way to write markup in your React components. It's a powerful syntax extension that fully unlocks the declarative and component-based nature of React. Understanding its rules and how it bridges the gap between JavaScript logic and UI representation is the most critical step in mastering React.

The 3 Golden Rules of JSX

While JSX looks like HTML, it has a few strict rules you must follow, which are enforced by the compiler:

  • Return a Single Root Element: A component's `return` statement can only return one "top-level" element. If you need to return multiple elements, wrap them in a parent `<div>` or, more commonly, a Fragment (<>...</>), which doesn't add an extra node to the DOM.
  • All Tags Must Be Closed: Unlike HTML, JSX is strict. Any tag you open must be closed. Tags that are "self-closing" in HTML, like `<img>` or `<br>`, *must* be self-closed in JSX: `<img />`, `<br />`.
  • Use `camelCase` for Props: JSX attributes are JavaScript props. Since `class` and `for` are reserved keywords in JavaScript, JSX uses `className` and `htmlFor` instead. This applies to many attributes: `onclick` becomes `onClick`, `tabindex` becomes `tabIndex`.

Embedding JavaScript with ``

The true power of JSX comes from the curly braces: ``. Inside them, you can write **any valid JavaScript expression**. This allows you to make your UI dynamic.

  • Displaying Data: `<h1>{user.name}</h1>`
  • Setting Attributes: `<img src={user.avatarUrl} />`
  • Conditional Logic (Ternary): `<p>{isLoggedIn ? 'Welcome back!' : 'Please log in'}</p>`
  • Mapping Arrays to Elements: The `.map()` method is the standard way to render lists:
<ul>
  {items.map(item => (
    <li key={item.id}>{item.name}</li>
  ))}
</ul>

Notice the `key` prop. This is a special prop that React uses to identify which items have changed, been added, or been removed, allowing for highly efficient updates.

Under the Hood: JSX to `React.createElement()`

Browsers do not understand JSX. Your code only runs because a **compiler** (like Babel) transforms your JSX before it ever reaches the browser.

When Babel sees this JSX:

const element = <h1 className="title">Hello</h1>;

It transforms it into this plain JavaScript function call:

const element = React.createElement("h1", { className: "title" }, "Hello");

This function, `React.createElement()`, doesn't create a DOM node. It returns a simple JavaScript **object** (a "React Element") that describes what *should* be rendered.

// The object returned looks like this:
{
  type: 'h1',
  props: {
    className: 'title',
    children: 'Hello'
  }
}

React then takes this object, compares it to the previous one, and calculates the most efficient way to update the actual browser DOM to match. This object-based intermediate step is the foundation of the Virtual DOM and is what makes React so fast.

Key Takeaway: JSX is a developer-friendly syntax that allows you to describe UIs declaratively. It is compiled into `React.createElement()` calls, which produce JavaScript objects. React uses these objects to efficiently manage and update the DOM.

React & JSX Glossary

JSX (JavaScript XML)
A syntax extension for JavaScript that allows you to write HTML-like markup inside your JavaScript files. It is the standard way to define UI in React.
Compiler / Transpiler (Babel)
A tool that converts code from one syntax to another. Babel is the most common transpiler used to convert modern JavaScript and JSX into older, browser-compatible JavaScript.
`React.createElement()`
The core JavaScript function that JSX compiles down to. It takes three arguments: `(type, props, ...children)` and returns a React Element object.
React Element
A plain, lightweight JavaScript object that describes what should appear on the screen. It is the central unit of React's Virtual DOM. Example: `{ type: 'h1', props: { ... } }`.
Component
A reusable, self-contained piece of UI, usually written as a JavaScript function or class. A function component is a function that accepts `props` and returns a React Element (JSX).
Props (Properties)
A system for passing data from a parent component to a child component. Props are read-only and are passed as attributes in JSX (e.g., `<User name="Ada" />`).
Fragment (`<>...</>`)
A special React component that allows you to return multiple elements from a component without adding an extra, unnecessary wrapper node to the DOM.
Expression (in JSX)
Any valid JavaScript code wrapped in curly braces `` that is evaluated and its result is rendered in the UI. You cannot use

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of frontend experts, who use React and JSX daily to build robust, high-performance web applications.

Verification and Updates

Last reviewed: November 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest React documentation and reflects industry best practices for 2025.

External Resources

Found an error or have a suggestion? Contact us!