JSX (JavaScript XML) is a syntax extension for JavaScript used in React. It allows us to write HTML-like structures directly inside our JavaScript code.
<h1>Hello React</h1>
Developer writes JSX → Babel converts it to React.createElement() → Browser displays the UI.
// Correct ✅
return (
<div>
<h1>Hello</h1>
<p>Welcome</p>
</div>
);
Since class is a reserved keyword in JS, JSX uses className.
<div className="box"></div>
All tags must be closed, including self-closing tags like <img /> or <br />.
<img src="logo.png" />
You can embed any JavaScript expression inside { }.
const name = "John";
<h1>Hello {name}</h1>
function App() {
const name = "Ravi";
return (
<div>
<h1>Welcome to React</h1>
<p>Hello {name}</p>
</div>
);
}
export default App;
Output: Welcome to React
Hello Ravi
You can perform math or logic directly inside the JSX structure.
function App() {
const a = 5;
const b = 3;
return (
<h1>Sum = {a + b}</h1>
);
}
Output: Sum = 8
| Feature | HTML | JSX |
|---|---|---|
| Syntax | Standard Markup | HTML inside JS |
| Attribute | class | className |
| Closing | Optional | Required |
| JS Inside | Not Allowed | Allowed via { } |
| Feature | JS DOM | JSX |
|---|---|---|
| UI Creation | Manual / Tedious | Declarative / Simple |
| Code Length | Long | Short |
| Readability | Hard to follow | Easy (looks like HTML) |
// JS DOM
const el = document.createElement("h1");
el.innerText = "Hello";
// JSX
<h1>Hello</h1>
React Setup → Components → JSX (Complete)
↓
Next: Props → State → Hooks