JSX stands for JavaScript XML. It is an XML/HTML-like syntax extension to ECMAScript designed by the React team. Rather than separating technologies by putting HTML markup in one file and JavaScript logic in another, React unites them into tightly coupled units called Components.
Web browsers cannot interpret JSX natively. Build tools (such as Babel, esbuild, or SWC) compile every JSX tag into standard JavaScript React.createElement() invocations:
| What You Write (JSX) | What React Compiles (JavaScript) |
|---|---|
|
|
React.createElement() creates a lightweight, plain JavaScript object representing the DOM node. These objects form the nodes of the Virtual DOM tree.
A component cannot return adjacent sibling elements without a parent container. Wrap them inside a standard <div> or a clean React Fragment (<>...</>):
// ❌ INVALID: Will throw a compilation error
return (
<h1>Title</h1>
<p>Paragraph</p>
);
// ✅ VALID: Wrapped in a React Fragment (leaves zero garbage tags in DOM)
return (
<>
<h1>Title</h1>
<p>Paragraph</p>
</>
);
Unlike traditional HTML where void elements like <img>, <input>, and <br> can remain unclosed, JSX follows strict XML grammar. Every tag must be explicitly self-closing with a trailing slash:
// ❌ Invalid in JSX: <img src="pic.jpg"> <br> <input type="text">
// ✅ Valid in JSX:
<img src="pic.jpg" alt="Profile" />
<br />
<input type="text" placeholder="Enter Name" />
Because JSX is closer to JavaScript than HTML, React DOM attributes follow camelCase naming conventions:
class becomes classNamefor becomes htmlForonclick becomes onClicktabindex becomes tabIndexstroke-width becomes strokeWidthAny valid JavaScript expression (variables, function calls, arithmetic calculations, ternary operators) can be executed directly inside JSX by wrapping it in curly braces { }:
function UserProfile() {
const firstName = "Priya";
const marks = [85, 92, 78];
const calculateAverage = (arr) => arr.reduce((a, b) => a + b, 0) / arr.length;
return (
<div className="profile-box">
<h2>Student: {firstName.toUpperCase()}</h2>
<p>Semester Score: {calculateAverage(marks).toFixed(1)}%</p>
<p>Status: {calculateAverage(marks) >= 50 ? "PASSED" : "FAILED"}</p>
</div>
);
}
Inside { }, you can ONLY place JavaScript expressions (code that evaluates to a value). You CANNOT place JavaScript statements like if (x) { ... }, for loops, or variable declarations inside JSX. Instead, use ternary operators (condition ? a : b) or logical AND (condition && a).
In standard HTML, you write styles as strings: style="color:red; font-size:20px". In JSX, inline styles are written as JavaScript objects using double curly braces (the outer brace enters JavaScript, the inner brace creates the object literal):
const customStyle = {
backgroundColor: '#5dbe59',
color: '#ffffff',
padding: '12px 24px',
borderRadius: '8px',
fontSize: '16px',
fontWeight: 'bold'
};
function StyledBanner() {
return <div style={customStyle}>Active Notification Banner</div>;
}
Now that you know how to write JSX fluently, let us explore how to structure functional and class components in React Components Guide.
Accelerate your React mastery by studying these tightly connected core tutorials:
Composing JSX into functional component structures.
Passing data into JSX elements and components.
Using ternary and logical && inside JSX expressions.
Iterating arrays into JSX lists using Array.map().