JSX Basics — React

JSX (JavaScript XML) is HTML-like writing inside JavaScript. Before the code runs, every JSX tag is turned into the function call React.createElement(type…

In the previous lesson, App returned <h1>Hello React!</h1>. It looks like HTML, yet it's written inside a JavaScript function. This HTML-like writing is called JSX, short for JavaScript XML. Almost every piece of UI in React is written with JSX.

Browsers don't understand JSX. So before the code runs, a build tool (a program that prepares your code), such as Vite, turns every JSX tag into an ordinary JavaScript function call.

JSX can also hold JavaScript values. Think of a printed invitation template: the sentence is already printed, and each guest's name goes into a blank box. In JSX, the printed sentence is the HTML-like writing. The blank box is written with curly braces {}, and it holds a JavaScript value, such as the value of a variable.

What does JSX turn into?

Take this small component:

function App() {
  const name = "Budi"
  const points = 3

  return (
    <div className="card">
      <h2>Hello, {name}!</h2>
      <p>You have {points * 2} points.</p>
    </div>
  )
}

The build tool turns the JSX inside return into this JavaScript code:

React.createElement(
  "div",
  { className: "card" },
  React.createElement("h2", null, "Hello, ", name, "!"),
  React.createElement("p", null, "You have ", points * 2, " points.")
)

Then React builds this DOM on the page:

<div class="card">
  <h2>Hello, Budi!</h2>
  <p>You have 6 points.</p>
</div>

Curly braces: where JavaScript values go

Before you look at the table below, take a guess. You have const name = "Budi", then you write <h2>Hello, name!</h2>, with no curly braces. Does it show Hello, Budi! or Hello, name!?

With name = "Budi":

JSX What shows
<h2>{name}</h2> Budi
<h2>name</h2> name
<p>{2 + 3}</p> 5
<p>{name.toUpperCase()}</p> BUDI
<p>{name === "Budi" ? "Hi boss" : "Hi"}</p> Hi boss

Without curly braces, name is just plain text. So <h2>Hello, name!</h2> shows Hello, name!, not Hello, Budi!.

Only expressions are allowed inside {}: code that produces one value. Examples are a variable, a calculation, a function call, or a ternary condition ? a : b. Statements such as if, for or const can't go there, because a statement is a command that doesn't produce a value. So {if (login) { ... }} fails to compile (the build tool can't turn it into JavaScript).

JSX rules that differ from HTML

  1. One outermost element. return can only give back one value, and each element is one value. So two side-by-side elements must be wrapped in one wrapper element. Use a <div>, or a Fragment <>...</> when you don't want an extra element in the DOM.
  2. Every tag is closed. A tag with nothing inside is closed with a / before the >: <img />, <br />, <input />. HTML lets you write <br>; JSX needs the /.
  3. className, not class. htmlFor, not for. class and for are keywords in JavaScript (words that already have their own meaning).
  4. Two-word attributes use camelCase. The second word starts with a capital letter: onClick, tabIndex, maxLength. The style attribute takes an object, so it has two pairs of braces: style={{ color: "red" }}. The outer braces mark a JavaScript value, and the inner braces are the object.
  5. Comments are written {/* ... */}. HTML-style comments, <!-- -->, don't work in JSX.

This component uses all five rules:

function App() {
  return (
    <>
      {/* Fragment: no extra div in the DOM */}
      <h1 className="title">Budi's Shop</h1>
      <img src="logo.png" alt="Budi's Shop logo" />
      <label htmlFor="email">Email</label>
      <input id="email" maxLength={40} />
      <p style={{ color: "gray" }}>Open every day.</p>
    </>
  )
}

⚠️ Slips that break JSX or make it show the wrong thing

TL;DR: JSX (JavaScript XML) is HTML-like writing inside JavaScript. Before the code runs, every JSX tag is turned into the function call React.createElement(type, attributes, ...children), then React builds the DOM from the result. Curly braces {} insert a JavaScript value, and only expressions (code that produces one value) can go inside them, not if or for. Without {}, a variable name shows as plain text. The other rules: one outermost element per return (wrap in a <div> or a Fragment <>...</>, a wrapper that doesn't go into the DOM), every tag closed (<img />), className and htmlFor instead of class and for, two-word attributes in camelCase, style={{ ... }} taking an object, and comments written {/* ... */}.

⚡ Curly braces hold JavaScript values

Write:

function App() {
  const name = "Budi"
  return <h2>Hello, {name}!</h2>
}
Guess first: written as <h2>Hello, name!</h2>, with no curly braces, what shows?

Hello, name!. Without {}, name is just plain text. With {name}, it shows Hello, Budi!.

Meaning: The HTML-like writing inside this function is called JSX. Curly braces {} insert a JavaScript value into it. What's inside is calculated first, and the result is what shows: <p>{2 + 3}</p> shows 5.

⚡ Only expressions inside {}

Meaning: Code that produces one value is called an expression: a variable, a calculation, a function call, or a ternary. Only expressions can go inside {}. if and for are commands that produce no value. So an if or a for inside {} fails to compile (it can't be turned into plain JavaScript). Use a ternary: {login ? "Hi" : "Please sign in"} shows Hi when login is true, and Please sign in when it is false.

⚡ One outermost element

Guess first: why does return <h1>Hello</h1><p>World</p> fail to compile?

return can only give back one value, and two elements are two values. So the two elements must be wrapped in one wrapper element (a parent): a <div> or a Fragment <>...</>.

Meaning: A Fragment <>...</> is a wrapper that doesn't add an element to the DOM.

⚡ Attributes the JSX way

Write:

<h1 className="title">Budi's Shop</h1>
<img src="logo.png" alt="Budi's Shop logo" />
<label htmlFor="email">Email</label>

Meaning: class and for are JavaScript keywords (words that already have their own meaning). So JSX uses className instead of class, and htmlFor instead of for. Every tag must be closed, even a tag with nothing inside, like <img />.

What you will learn