Understanding JSX in React: A Friendly Guide



This content originally appeared on DEV Community and was authored by Tijani Olagunju

Introduction

Welcome! If you’re learning React, you’ve likely encountered something that looks like HTML wrapped in JavaScript — that’s JSX. At first glance, JSX can feel confusing. You might ask, “Is this JavaScript? Is it HTML? Why is it here?”

This guide is here to support you through that uncertainty. We’ll walk through JSX together with a clear focus on understanding the why and the how, not just the what. By the end, JSX will feel like a familiar and powerful friend — not a mysterious stranger.
Let’s dive in with clarity and confidence.

Table of Contents

  1. What is JSX?
  2. Why Use JSX in React?
  3. JSX vs HTML: Key Differences
  4. Embedding JavaScript in JSX
  5. JSX Best Practices
  6. Common Mistakes and How to Avoid Them
  7. Conclusion and Encouragement

1. What is JSX?

JSX stands for JavaScript XML. It’s a syntax extension for JavaScript, and it’s used with React to describe what the UI should look like.

In simpler terms, JSX lets you write HTML-like code inside your JavaScript.

Example:

const element = <h1>Hello, world!</h1>;

Behind the scenes, JSX is transformed into regular JavaScript:

const element = React.createElement('h1', null, 'Hello, world!');

This transformation happens during the build process, so you can write declarative code that’s easy to read and reason about.

2. Why Use JSX in React?

JSX might seem optional, but it brings several benefits:
✅ Declarative Syntax

JSX lets you describe what you want to render, rather than how to create it step by step.
✅ Familiarity

If you’ve written HTML before, JSX feels comfortable — you’re already halfway there.
✅ Tighter Integration

JSX enables tight integration between layout and logic. You don’t have to switch contexts between languages.

3. JSX vs HTML: Key Differences

Even though JSX looks like HTML, it has some important differences:

1. className instead of class

// Wrong
<div class="container">Hello</div>

// Right
<div className="container">Hello</div>

This is because class is a reserved word in JavaScript.

2. Self-Closing Tags Required

JSX requires tags that don’t have children to be self-closed:

// Wrong
<input>

// Right
<input />

3. camelCase for Event Handlers and Attributes

// HTML
<button onclick="handleClick()">Click</button>

// JSX
<button onClick={handleClick}>Click</button>

4. Embedding JavaScript in JSX

You can embed JavaScript expressions inside JSX using curly braces {}. This is one of JSX’s most powerful features.

Example:

const name = "Alice";
const greeting = <h1>Hello, {name}!</h1>;

You can also use conditional rendering:

const isLoggedIn = true;
const message = <p>{isLoggedIn ? "Welcome back!" : "Please sign in."}</p>;

Or loop through data:

const items = ["Apple", "Banana", "Cherry"];
const list = (
  <ul>
    {items.map(item => <li key={item}>{item}</li>)}
  </ul>
);

JSX is expressive and blends logic with markup beautifully.

5. JSX Best Practices

Here are a few friendly tips to keep your JSX clean and effective:
✅ Use Meaningful Names

Name components and variables clearly.

const WelcomeMessage = () => <h1>Welcome!</h1>;

✅ Keep Components Small

Break complex UIs into small, reusable components.
✅ Use Fragments When Needed

If a component returns multiple elements, wrap them in <>...</> instead of an unnecessary <div>.

<>
  <h1>Hello</h1>
  <p>Welcome back.</p>
</>

6. Common Mistakes and How to Avoid Them

Here are some common pitfalls beginners face — and how to steer clear:
❌ Forgetting to Return in Arrow Functions

// Wrong
const MyComponent = () => {
  <div>Hello</div>;
};

// Right
const MyComponent = () => {
  return <div>Hello</div>;
};

// Or use implicit return:
const MyComponent = () => <div>Hello</div>;

❌ Using if/else Inside JSX

JSX doesn’t support full statements (like if). Use ternary operators or move logic outside.

// Not allowed
{ if (loggedIn) { return <p>Hi!</p>; } }

// Instead
{loggedIn ? <p>Hi!</p> : <p>Please log in.</p>}

❌ Not Using Keys in Lists
When rendering lists, always provide a key to each item.

items.map(item => <li key={item}>{item}</li>);

7. Conclusion and Encouragement

JSX is a cornerstone of React development, and it’s perfectly normal to feel a little unsure at the start. The good news? JSX becomes intuitive with practice. Every time you write a line of JSX, you’re building both your understanding and your confidence.

As you continue learning React, keep experimenting, breaking things, and building small projects. You’ll be amazed how quickly JSX becomes second nature.

And remember — you’re doing great.


This content originally appeared on DEV Community and was authored by Tijani Olagunju