JSX Basics
โš›๏ธ React 18+ ๐ŸŸข Chapter 5 of 39 ๐Ÿ“‚ Phase 03: JSX and UI ๐Ÿ“… 2026 Edition
๐Ÿ“Œ Covered in this chapter: JSX vs HTML ยท Single Parent Element ยท Fragments ยท Expressions in {} ยท className ยท Attributes ยท Self-Closing Tags ยท Comments
JSX looks like HTML but is actually JavaScript in disguise โ€” understanding this distinction unlocks nearly everything else about writing React components confidently.
1JSX Is JavaScript, Not HTML

Behind the scenes, JSX is compiled into regular JavaScript function calls. Writing <h1>Hello</h1> actually compiles to something like React.createElement("h1", null, "Hello") โ€” this is exactly why JSX rules differ from HTML in a few important ways covered below.

2Embedding JavaScript with Curly Braces
๐Ÿ’ป Example 1: JavaScript Expressions Inside JSX
const name = "Ravi";

function App() {
  return (
    <div>
      <h1>Hello, {name}</h1>
      <p>Welcome to React.</p>
      <p>2 + 2 = {2 + 2}</p>
    </div>
  );
}
๐Ÿ” Key Rule:

Anything inside curly braces {} is evaluated as a regular JavaScript expression โ€” variables, math, function calls, ternaries โ€” and the result is inserted directly into the rendered output.

3One Parent Element and Fragments

A component can only return one single root element. Trying to return two sibling elements side-by-side causes a compile error:

// โŒ Error - two root elements
function Bad() {
  return (
    <h1>Title</h1>
    <p>Text</p>
  );
}

// โœ… Wrapped in a Fragment - no extra HTML element added to the page
function Good() {
  return (
    <>
      <h1>Title</h1>
      <p>Text</p>
    </>
  );
}

The empty <>...</> tags are a Fragment โ€” a way to group multiple elements without adding an unnecessary wrapping <div> to the actual page.

4className, Self-Closing Tags, and JSX Comments
function App() {
  return (
    <>
      {/* JSX comments look like this */}
      <div className="container">
        <img src="logo.png" />
        <br />
      </div>
    </>
  );
}

JSX uses className instead of HTML's class attribute (because class is a reserved word in JavaScript), and self-closing elements like <img /> and <br /> always need the trailing slash.

โš ๏ธ Using class Instead of className

Writing <div class="box"> instead of <div className="box"> doesn't crash your app, but it silently fails to apply the styling โ€” React simply ignores the unrecognized class attribute in JSX. This is one of the most common early mistakes for developers coming from plain HTML.

๐Ÿ’ป Hands-on Interactive Practice Challenge

Write a component that displays your name, age, and the result of a simple calculation, all using curly brace expressions, wrapped correctly in a single root element.

React Practice Challenge โ–ถ Run in Compiler
function Profile() {
  const name = "Alex";
  const age = 22;

  return (
    <>
      <h2>{name}</h2>
      <p>Age next year: {age + 1}</p>
    </>
  );
}

export default Profile;
Run This Challenge in Online JS IDE โ†’
โ“ Frequently Asked Questions (FAQ)

Q Can I put an if statement directly inside JSX curly braces?

No โ€” curly braces only accept expressions, not statements. You can use a ternary operator or logical && instead (covered fully in Chapter 9: Conditional Rendering), or compute the value before the return statement.

Q Why can't a component return two sibling elements without a wrapper?

JSX compiles to a single function call tree, and a function can only return one value. Fragments (<>) solve this by grouping multiple elements into one without adding extra markup to the actual page.

OC
Written by Our Compiler Technical Editorial Team
Reviewed for accuracy & tested on React 18+ ยท Last updated August 2026