What Is JSX in React? Explained for Beginners
New to React? Learn what JSX is, why React uses it, and how to write your first JSX code with simple, line-by-line examples for total beginners.
New to React? Learn what JSX is, why React uses it, and how to write your first JSX code with simple, line-by-line examples for total beginners.
If you have started learning React, you have probably seen code that looks like HTML sitting right inside a JavaScript file. That mix is called JSX, and it can look strange the first time you see it. In plain words: JSX is a special syntax that lets you write HTML-like markup inside your JavaScript code, so you can describe what a piece of your webpage should look like without leaving your JavaScript file. This guide explains what JSX is, why React uses it, and how to write your first JSX code, one small example at a time.
JSX stands for JavaScript XML. It is not a separate language. It is an extension to JavaScript that lets you write tags like <div> or <p> directly in your JavaScript code, the same way you would write them in an HTML file. Browsers cannot read JSX directly. A tool called a compiler (usually Babel, working behind the scenes when you use Create React App or Vite) turns your JSX into plain JavaScript before the browser ever sees it.
In short: you write JSX because it is easier for humans to read and write, and the build tools convert it into JavaScript that the browser understands.
React's whole job is to describe what your page should look like, and JSX makes that description easy to write and easy to read. Here is why most React developers prefer it:
Here is a tiny JSX example. It creates a heading and a paragraph, and stores them in a variable called greeting.
function Greeting() {
const greeting = (
<div>
<h1>Hello, world!</h1>
<p>Welcome to your first JSX example.</p>
</div>
);
return greeting;
}Line by line: function Greeting() defines a React component, which is just a JavaScript function that returns some markup. Inside it, const greeting = (...) stores a block of JSX in a variable, wrapped in parentheses purely so the multi-line code stays readable. The <div> tag wraps everything inside it, because JSX requires a single root element (more on that rule below). The <h1> and <p> tags work exactly like they do in HTML. Finally, return greeting; hands that markup back to React so it can be shown on the page.
JSX looks like HTML, but it has its own strict rules. Beginners usually hit these four first.
You cannot return two sibling tags side by side. They must share a single parent.
// This causes an error: two top-level tags
return (
<h1>Hello</h1>
<p>Welcome</p>
);
// This works: both tags share one parent
return (
<div>
<h1>Hello</h1>
<p>Welcome</p>
</div>
);The first block fails because React cannot return two separate elements at once. The second block fixes this by wrapping both tags inside a single <div>. If you do not want an extra <div> in your final HTML, you can use an empty tag written as <>...</> instead, called a Fragment; it groups elements without adding any extra markup.
In plain HTML, tags like <img> or <br> can be left open. In JSX, every tag must close itself, usually by adding a slash before the final angle bracket.
<img src="cat.png" alt="A cat" />
<br />
<input type="text" />Notice the /> at the end of each tag. That slash tells JSX the tag is self-closing, meaning it has no children and does not need a separate closing tag.
A handful of HTML attributes are renamed in JSX because they clash with reserved JavaScript words or naming conventions.
| HTML attribute | JSX attribute | Why it changes |
|---|---|---|
| class | className | class is a reserved word in JavaScript |
| for | htmlFor | for is a reserved word in JavaScript |
| onclick | onClick | JSX event names use camelCase |
| tabindex | tabIndex | JSX attribute names use camelCase |
A component's JSX ultimately has to evaluate to a single value that React can render, which is exactly why rule 1 (one root element) exists in the first place.
The real power of JSX is that you can drop plain JavaScript into your markup by wrapping it in curly braces { }. Anything inside the braces is treated as JavaScript, not text.
function UserCard() {
const name = "Amara";
const age = 27;
return (
<div className="card">
<h2>{name}</h2>
<p>Age: {age}</p>
<p>Next year, {name} will be {age + 1}.</p>
</div>
);
}Here, name and age are normal JavaScript variables defined above the return statement. Inside the JSX, {name} and {age} insert the current value of each variable into the page. The line {age + 1} shows that you can put any JavaScript expression inside the curly braces, including simple math, and JSX will display the result. Note that className is used instead of class, following the rule covered above.
It helps to know that JSX is just a shortcut. Every JSX tag actually compiles down to a plain JavaScript function call named React.createElement(). Here is the same code written both ways.
// What you write with JSX
const element = <h1 className="title">Hello!</h1>;
// What it becomes after compiling (roughly)
const element = React.createElement(
"h1",
{ className: "title" },
"Hello!"
);React.createElement("h1", { className: "title" }, "Hello!") takes three pieces: the tag name ("h1"), an object of attributes ({ className: "title" }), and the content inside the tag ("Hello!"). JSX simply lets you skip writing this function call by hand and use familiar HTML-like tags instead. You do not need to memorize createElement; it is just useful to know that JSX is not magic, it is a shorter way to write regular JavaScript.
You do not need to install anything to experiment. Open an online playground like CodeSandbox or StackBlitz, choose a React template, and try changing the UserCard example above: add a new field, insert a curly-brace expression, or intentionally break one of the JSX rules to see the error message React gives you. Making mistakes on purpose is one of the fastest ways to understand a rule.
Now that you understand JSX, see how it is used to build reusable pieces of a page.
Read: What Is a React Component?No. JSX looks a lot like HTML, but it is actually JavaScript syntax that gets compiled into React.createElement() calls. That is why some attribute names differ, such as className instead of class, and why every tag must be properly closed.
No, JSX is optional. You could write React.createElement() calls directly, but almost every React developer uses JSX because it is far easier to read and write, especially for larger pages.
React requires every component to return a single root element. If you have two or more tags side by side without a shared parent, wrap them in a <div> or an empty Fragment (<>...</>) to fix the error.
Yes, but only expressions, not full statements. Wrap the expression in curly braces, like {price * quantity} or {isLoggedIn ? "Welcome" : "Please log in"}. Things like if statements or for loops need to go outside the JSX, before the return statement.
class is a reserved keyword in JavaScript, used for defining classes. Since JSX compiles to JavaScript, React renamed the HTML class attribute to className to avoid a naming conflict.
JSX can feel unfamiliar for the first day or two, but it quickly becomes second nature: HTML-like tags for structure, curly braces whenever you need real JavaScript. Once it clicks, you will find it faster to read a component's JSX than to imagine the same UI described in separate HTML and JavaScript files.
Building a product and want a team that knows React inside and out?
Talk to FepiqOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
Learn SQL CASE WHEN step by step. This beginner guide shows how to label, group and sort data with simple examples you can copy and run today.
PostgreSQL ILIKE explained for beginners: search text without worrying about case, learn % and _ wildcards, and copy simple examples you can run today.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.