React JSX Syntax: JavaScript XML Rules & Best Practices

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.

Under the Hood: How JSX Compiles to Pure JavaScript

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)
const element = (
  <h1 className="title">
    Hello World!
  </h1>
);
const element = React.createElement(
  'h1',
  { className: 'title' },
  'Hello World!'
);

React.createElement() creates a lightweight, plain JavaScript object representing the DOM node. These objects form the nodes of the Virtual DOM tree.

The 5 Golden Rules of JSX Syntax

Rule 1: Return a Single Root Element

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>
  </>
);

Rule 2: Close Every Single Tag

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" />

Rule 3: Use camelCase for Attributes

Because JSX is closer to JavaScript than HTML, React DOM attributes follow camelCase naming conventions:

  • class becomes className
  • for becomes htmlFor
  • onclick becomes onClick
  • tabindex becomes tabIndex
  • stroke-width becomes strokeWidth

Rule 4: Embedding JavaScript Expressions with Curly Braces { }

Any 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>
  );
}

Common Student Mistake: Statements vs Expressions

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).

Rule 5: Inline Styles Use JavaScript Objects

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>;
}

Next Step: Components Mastery

Now that you know how to write JSX fluently, let us explore how to structure functional and class components in React Components Guide.

Explore Related React JS Tutorials

Accelerate your React mastery by studying these tightly connected core tutorials:

Components Architecture →

Composing JSX into functional component structures.

Props & Attributes →

Passing data into JSX elements and components.

Conditional Rendering →

Using ternary and logical && inside JSX expressions.

Lists & Keys Reconciliation →

Iterating arrays into JSX lists using Array.map().

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!