If you’ve just started learning React, you’ve probably seen code that looks like HTML inside JavaScript — something like this:
function Welcome() {
return <h1>Hello, React!</h1>;
}
At first glance, that doesn’t look like regular JavaScript — and that’s because it isn’t!
It’s JSX, one of React’s most powerful and beginner-friendly features.
Let’s explore what JSX is, why it exists, and how it connects to the Virtual DOM, the secret behind React’s speed.
🧩 What Is JSX?
JSX stands for JavaScript XML.
It allows you to write HTML-like code inside JavaScript to describe what the user interface should look like.
Here’s a simple example:
const element = <h1>Hello, world!</h1>;
Under the hood, React doesn’t actually understand HTML — this code is converted into plain JavaScript like this:
const element = React.createElement('h1', null, 'Hello, world!');
So JSX is not magic — it’s just syntactic sugar (a simpler way of writing code) that gets transpiled into standard JavaScript by tools like Babel.
⚙️ Why JSX Is Useful
JSX helps developers:
- 🧠 Think visually — you can see your UI layout right in your code.
- 📦 Combine markup and logic — so you can easily link dynamic data with your interface.
- 🚀 Write cleaner, faster code — fewer
document.createElement()calls and DOM manipulations.
Here’s a quick example showing how JSX uses dynamic data:
const name = "June";
const greeting = <h1>Hello, {name}!</h1>;
When rendered, React replaces {name} with the actual value — producing:
👉 Hello, June!
🧱 The Rules of JSX
JSX looks simple, but it follows some rules:
- Use one parent element — every JSX block must be wrapped in a single tag.
return ( <div> <h1>Welcome</h1> <p>React is fun!</p> </div> ); - Use
classNameinstead ofclass— because “class” is a reserved JavaScript word. - Use curly braces
{}for JavaScript expressions — to insert variables or logic inside JSX.
⚡ What Is the Virtual DOM?
Now that you know how JSX defines your UI, let’s see how React updates it so quickly — thanks to the Virtual DOM.
The DOM (Document Object Model) is how the browser represents a web page.
But updating the real DOM directly can be slow, especially when you have many elements changing frequently.
React solves this with the Virtual DOM — a lightweight copy of the real DOM stored in memory.
🔄 How the Virtual DOM Works
Here’s what happens behind the scenes when you update something in React:
- You write JSX — React creates a Virtual DOM tree based on it.
- When something changes (like a user clicks a button), React:
- Creates a new Virtual DOM reflecting that change.
- Compares the new Virtual DOM with the previous one (this step is called diffing).
- Updates only the changed parts of the real DOM — nothing else.
This process is called Reconciliation, and it’s why React feels so fast and responsive.
🧠 JSX + Virtual DOM = React Magic
Together, JSX and the Virtual DOM make React powerful:
- JSX lets you describe your UI in a natural, visual way.
- The Virtual DOM makes updating your UI efficient by touching only what’s changed.
For example, if you update a single item in a long list, React will re-render only that item, not the whole page.
This means faster apps, smoother interactions, and better performance.
🚀 In a Nutshell
- JSX lets you write HTML-like code inside JavaScript — it’s easy to read and maintain.
- The Virtual DOM keeps React fast by updating only what changes, not the whole page.
- Combined, they make React both developer-friendly and user-friendly.
Once you’re comfortable with these two ideas, you’re ready to dive deeper into components, props, and state — the real building blocks of React apps.