What is JSX in JavaScript XML
Explore JSX, a JavaScript syntax extension blending XML like markup with JavaScript to describe UI. Learn how it works, how it's compiled, and when to use it in React projects with practical explanations and examples.

JSX is a JavaScript syntax extension that lets you embed XML‑like markup inside JavaScript files. It is transformed by build tools into standard React.createElement calls.
JSX: A JavaScript Syntax Extension
JSX is a JavaScript syntax extension that lets you write XML‑like markup inside JavaScript files. It is not HTML by itself; it is a compile‑time convenience that describes UI structure. When you run your code through a build tool (usually Babel or TypeScript with a React preset), JSX is transformed into plain JavaScript calls that create UI elements. In practice, developers often ask what is JSX in the context of JavaScript XML, and the answer is that JSX blends familiar markup with JavaScript logic to express components more clearly. The result is a syntax that reads like HTML while remaining fully within the JavaScript ecosystem.
- It is primarily used with React and related libraries, but the concept has influenced other UI toolchains.
- The transformation step is what makes JSX usable in browsers that do not understand JSX syntax directly.
Understanding JSX helps you see how UI components become composable, reusable units, and why many projects describe JSX as a bridge between markup and behavior.
The Syntax You See in JSX
JSX resembles HTML or XML, but it follows JavaScript rules and has its own quirks. Here are the core patterns you’ll encounter:
- Self‑closing tags are valid for void or empty children, e.g., <img /> or <Input />.
- Attributes map to props; note the camelCase naming for DOM props (for example, className, htmlFor).
- Curly braces embed JavaScript expressions inside markup, e.g., <span>{user.name}</span> or <List items={array} />.
- You can nest elements to build component trees; JSX elements can be strings, numbers, or React components.
Example:
const element = <div className="container">Hello, {name}!</div>;This markup looks like HTML but is transformed into JavaScript function calls, enabling dynamic rendering and component composition.
How JSX is Transformed: From JSX to JavaScript
JSX is not executed directly by browsers. A compiler or transpiler rewrites JSX into plain JavaScript calls that build the UI tree. In classic setups, a JSX snippet like <App name={user.name} /> becomes a sequence of React.createElement calls, which create element descriptors understood by the React rendering engine.
With newer tooling and React versions, there is also an automatic JSX runtime that inserts the necessary import or runtime function, depending on configuration. The key idea remains: JSX is a readable syntax that describes what UI should look like, and tooling translates that description into executable code.
This transformation is why JSX feels so close to HTML while still giving you full programmatic power inside JavaScript. It enables a declarative approach to UI construction without sacrificing JavaScript expressiveness.
Using JSX with React: Components and Props
At its core, JSX helps you write components that render UI based on props and state. A simple function component might look like this:
function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}This component receives a prop named name and renders a heading with a dynamic value. Components can accept children, compose with other components, and manage local state or side effects using hooks. JSX keeps the markup familiar while enabling powerful composition patterns.
Key ideas:
- Components are reusable building blocks that describe UI portions.
- Props flow data from parents to children, enabling predictable rendering.
- JSX can interpolate JavaScript expressions inside braces to reflect state, props, or computed values.
Common Pitfalls and Best Practices
To maximize clarity and maintainability when using JSX, keep these practices in mind:
- Do not confuse JSX with HTML. JSX attributes and event handling have JavaScript semantics and naming rules.
- Use className instead of class and htmlFor instead of for when referencing DOM attributes.
- Keep components small and focused; break complex UIs into smaller components to improve readability and reuse.
- Return a single root element from a component or use React Fragments to avoid extra wrappers.
- Prefer descriptive component and prop names to improve self‑documentation of your UI structure.
Common pitfalls include overusing inline styles, neglecting accessibility, and mixing presentational markup with logic in a way that reduces readability. Following these best practices helps keep JSX code maintainable over time.
Tooling and Setup for JSX
Getting started with JSX typically requires a build step. Common toolchains include Babel with a React preset, or modern bundlers like Vite or Create React App that wire JSX processing automatically. If you are using TypeScript, you’ll also configure tsconfig to allow JSX syntax. In short:
- Install a project scaffold or set up a bundler that understands JSX.
- Ensure your build pipeline includes a JSX/React plugin or preset.
- Configure appropriate runtime or automatic JSX transform if your setup supports it.
With the right tooling, writing JSX becomes a smooth, readable part of your development workflow, enabling rapid UI development without compromising JavaScript control flow.
A Minimal Real World Example: A Small App
Consider a tiny app that renders a greeting and a list of items using JSX:
import React from 'react';
function ItemList({ items }) {
return (
<ul>
{items.map((it, idx) => (
<li key={idx}>{it}</li>
))}
</ul>
);
}
function App() {
const fruits = ['Apple', 'Banana', 'Cherry'];
return (
<section>
<h1>Welcome to JSX Demo</h1>
<Greeting name="Alex" />
<ItemList items={fruits} />
</section>
);
}
function Greeting({ name }) {
return <p>Hi there, {name}!</p>;
}
export default App;This example demonstrates composing components, passing props, and rendering dynamic lists using JSX. It also shows how expressions and keys help React efficiently reconcile lists during updates.
Authority sources
JSX is discussed in depth in several authoritative sources. Useful references include the React documentation on introducing JSX, MDN coverage of JSX concepts, and Babel's JSX transformation guide:
- https://reactjs.org/docs/introducing-jsx.html
- https://developer.mozilla.org/en-US/docs/Web/JSX
- https://babeljs.io/docs/babel-plugin-transform-react-jsx
These sources provide complementary perspectives on how JSX works, how it integrates with React, and how tooling transforms JSX into executable JavaScript.
Questions & Answers
What is JSX and how is it different from HTML?
JSX resembles HTML but is a JavaScript syntax extension. It compiles to React.createElement calls and is used to describe UI in a React context. It is not HTML and cannot be directly parsed by the browser without a build step.
JSX looks like HTML but is JavaScript syntax that gets compiled to React calls. It is not HTML and needs a build step.
Is JSX required to write React components?
No, you can write React components using JavaScript without JSX by calling React.createElement directly. However, JSX is widely used because it is more readable and expressive for describing UI structure.
You can write React without JSX, but JSX is commonly used for readability.
Can JSX be used with libraries other than React?
JSX is a syntax extension and can be used with libraries other than React if those libraries provide a compatible JSX pragma or runtime. In practice, React remains the most common pairing.
Yes, JSX can work with other libraries if you set up a JSX runtime, though React is the most common pairing.
Does the browser run JSX directly?
No. Browsers do not understand JSX directly. A build step or compiler translates JSX into plain JavaScript before it runs in the browser.
JSX must be compiled to JavaScript before the browser can execute it.
What is className in JSX and why is it used?
In JSX the HTML class attribute is written as className because class is a reserved word in JavaScript. It maps to the DOM element's class attribute when rendered.
Use className in JSX to assign CSS classes; it maps to the actual class attribute in HTML.
How do I get started with JSX in a new project?
Start by choosing a build tool or framework (like Create React App or Vite) that supports JSX. Ensure your project is configured to transpile JSX code and then start writing components using JSX syntax.
Pick a setup that supports JSX, install it, and begin writing components using JSX.
What to Remember
- Learn JSX is a JavaScript syntax extension, not HTML
- JSX blends XML like markup with JavaScript logic
- Transpilers translate JSX into React.createElement calls
- Use JSX to write readable, component based UI code
- Remember attributes and expressions are JavaScript rules in JSX