DEV Community

Cover image for What is JSX in React? Understanding React’s Syntax
Shefali
Shefali

Posted on • Originally published at shefali.dev

8 1 1 1

What is JSX in React? Understanding React’s Syntax

If you’re learning React, then it’s essential to learn about JSX in React. JSX is a powerful feature of React which allows you to write HTML-like syntax in JavaScript. It makes React components more readable and maintainable.

In this post, you’ll learn everything about JSX, from its syntax to best practices.

Before we get started, don’t forget to subscribe to my newsletter!
Get the latest tips, tools, and resources to level up your web development skills, delivered straight to your inbox. Subscribe here!

Now let’s jump right into it!🚀

What is JSX?

JSX (JavaScript XML) is a syntax extension for JavaScript that allows you to write HTML inside JavaScript. It makes code cleaner compared to using React.createElement().

Example without JSX:

return (
  React.createElement("h1", {}, "Hello, World!");
);
Enter fullscreen mode Exit fullscreen mode

Example with JSX:

return (
  <h1>Hello, World!</h1>;
);
Enter fullscreen mode Exit fullscreen mode

Now you can see how JSX makes code more clean and readable.


Important JSX Rules

To write better JSX, keep these rules in mind:

1. One Parent Element is Required

It’s a must to wrap multiple elements inside a single parent element.

Incorrect:

return (
  <h1>Hello</h1>
  <p>Welcome to React!</p>
);
Enter fullscreen mode Exit fullscreen mode

Always wrap the elements in a <div> or <React.Fragment>.

Correct:

return (
  <div>
    <h1>Hello</h1>
    <p>Welcome to React!</p>
  </div>
);
Enter fullscreen mode Exit fullscreen mode

Or you can use short syntax (React Fragment <></>):

return (
  <>
    <h1>Hello</h1>
    <p>Welcome to React!</p>
  </>
);
Enter fullscreen mode Exit fullscreen mode

2. Write JavaScript Expressions inside {}

If you want to write any expression of JavaScript inside JSX, then wrap the JavaScript expression inside {}.

For example:

const name = "Shefali";

return <h1>Hello, {name}!</h1>;
Enter fullscreen mode Exit fullscreen mode

This will render "Hello, Shefali!".

Note: You cannot use statements like if-else directly inside {}.

3. Attribute Names Must Use CamelCase

In JSX, you can’t write HTML-style attributes. Instead, you have to use camelCase format.

For example: You can’t write onclick, instead write onClick.

// Incorrect:
return (
  <button onclick="handleClick()">Click Me</button>  // HTML-style syntax
);

// Correct:
return (
  <button onClick={handleClick}>Click Me</button>
);

Enter fullscreen mode Exit fullscreen mode

4. Inline Styles are JavaScript Objects

In JSX, the style attribute uses an object with camelCase properties.

For example:

const styleObj = { color: "blue", fontSize: "20px" };
return (
  <p style={styleObj}>Styled Text</p>;
);

// OR
return (
  <p style={{ color: "blue", fontSize: "20px" }}>Styled Text</p>;
);
Enter fullscreen mode Exit fullscreen mode

Note: Always use camelCase properties. For example, use backgroundColor not background-color.

5. Conditional Rendering in JSX

Use ternary operators or && for conditions.

Using a Ternary Operator:

const isLoggedIn = true;

return <h1>{isLoggedIn ? "Welcome Back!" : "Please Sign In"}</h1>;
Enter fullscreen mode Exit fullscreen mode

Using && Operator:

const showMessage = true;

return <>{showMessage && <p>Hello, User!</p>}</>;
Enter fullscreen mode Exit fullscreen mode

6. Rendering Lists in JSX

Use .map() to render lists, and always provide a key prop.

For example:

const names = ["Alice", "Bob", "Charlie"];

return (
  <ul>
    {names.map((name) => (
      <li key={name}>{name}</li>
    ))}
  </ul>
);
Enter fullscreen mode Exit fullscreen mode

7. class Attribute is className in JSX

In HTML, you use class, but in JSX, it’s className.

return <h1 className="heading">Hello, JSX!</h1>;
Enter fullscreen mode Exit fullscreen mode

8. Self-Closing Tags Must Have /

Self-closing elements like <img> and <input> must have a / in JSX.

return <img src="image.jpg" alt="Example" />;
Enter fullscreen mode Exit fullscreen mode

JSX Compilation Process

JSX doesn’t directly run in the browser. The Babel compiler converts JSX code into React.createElement().

JSX code:

return (
  <h1>Hello, JSX!</h1>;
);
Enter fullscreen mode Exit fullscreen mode

Babel converts it like this:

return (
  React.createElement("h1", {}, "Hello, JSX!");
);
Enter fullscreen mode Exit fullscreen mode

This proves that JSX is just syntactic sugar, making writing React code easier.

Why is Compilation Needed?

  • Browsers don’t understand JSX, but they understand JavaScript.
  • Babel translates JSX into React function calls, ensuring compatibility.
  • This optimizes performance by converting JSX into efficient JavaScript.

Using JSX in Functional Components

You can use JSX inside functional components.

function Welcome(props) {
  return <h1>Hello, {props.name}!</h1>;
}
Enter fullscreen mode Exit fullscreen mode

And this can be used in other components like this:

<Welcome name="Shefali" />
Enter fullscreen mode Exit fullscreen mode

This will render "Hello, Shefali!".


JSX vs Traditional JavaScript

Feature JSX (Recommended) Traditional JavaScript
Syntax <h1>Hello</h1> React.createElement('h1', {}, 'Hello')
Readability Clean & Simple Complex & Verbose
Performance Optimized via Babel Same (After Compilation)
Debugging Easy to Debug Harder to Read

🎯Wrapping Up

That’s all for today!

For paid collaboration connect with me at : connect@shefali.dev

I hope this list helps you. 🚀

If you found this post helpful, here’s how you can support my work:
Buy me a coffee – Every little contribution keeps me motivated!
📩 Subscribe to my newsletter – Get the latest tech tips, tools & resources.
𝕏 Follow me on X (Twitter) – I share daily web development tips & insights.

Keep coding & happy learning!

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay