JSX: HTML inside JavaScript

Intermediate

Introduction

JSX looks like HTML, which makes components easy to read, but it is JavaScript underneath. That gives it a few rules that HTML does not have, and one big power: anywhere in JSX you can open braces and write a JavaScript expression. This lesson covers the rules you will use in every component.

How it works

Braces put a JavaScript expression into JSX: a variable, a calculation, a function call or a condition.

<h2>{user.name.toUpperCase()}</h2>
<p>{user.online ? "Online now" : "Offline"}</p>
<p>{price * quantity} in total</p>

Only expressions work in braces, not statements: if and for are not allowed inside, so you use the conditional operator, or compute a value before the return. Text values are escaped automatically, so a user's name containing <script> is shown as text, never run.

Attributes use JavaScript names. class is a JavaScript keyword, so JSX writes className; for becomes htmlFor; attributes with two words are camelCase, as in tabIndex and onClick. A string attribute uses quotes, any other value uses braces: maxLength={20}. The style attribute takes an object, not a string, with camelCase property names, and plain numbers mean pixels: style={{ fontSize: 18, borderRadius: 6 }}. The double braces are one pair for "JavaScript here" and one for the object.

Every tag must be closed: <img src="a.png" alt="" /> and <br />, not <br>. A component must return one element. To return several side by side without wrapping them in a div, use a fragment, written <> and </>: it groups them and adds nothing to the page.

JSX is not understood by browsers. Before your code runs, it is transpiled into ordinary function calls: <h2 className="t">Hi</h2> becomes a call that creates an object describing an h2 with that class and text. That is why JSX can be stored in variables, returned from functions and passed around like any value. In this course the TypeScript compiler does this step in your browser whenever you press Run.

Example

App.jsx
console.log output from the page shows here.

Line by line

  1. user.initials and user.name.toUpperCase() are expressions in braces; their values are shown.
  2. The conditional operator chooses which text to show, because if is not allowed inside JSX.
  3. className sets the class, and style gets an object: the border, padding and corner radius.
  4. user.messages + 2 is worked out before it is shown: 5.
  5. The fragment <>...</> returns the card and the paragraph together without an extra element.

Expected result

The preview shows a bordered card with "AL", the name "ADA LOVELACE" and "Online now", and under it the line "5 new messages, 2026". The root holds exactly two elements, because the fragment adds none.

In the real world

Every React codebase is written in JSX, or TSX with TypeScript. Design systems pass style values as objects computed from themes, and conditional text in braces is how interfaces show states such as "3 items" or "Out of stock". Editors and linters understand JSX and point out a missing closing tag or a class written instead of className.

Common mistakes

  • Writing class= or for= instead of className and htmlFor.
  • Giving style a string: style="color: red" is an error; use style={{ color: "red" }}.
  • Putting an if statement inside braces; use the conditional operator or compute the value first.
  • Returning two elements side by side without a wrapper or a fragment.

Best practices

  • Keep expressions in braces short; compute anything complicated in a variable before the return.
  • Wrap multi-line JSX in parentheses after return.
  • Prefer CSS classes for styling and keep style objects for values that are computed.

Summary

JSX is JavaScript that looks like HTML. Braces embed expressions; attributes use JavaScript names such as className and htmlFor; style takes an object; every tag is closed; and a component returns one element or a fragment. Before running, JSX is transpiled into function calls that describe the elements.

Quick check

1. How do you set an element's CSS class in JSX?
2. Which is a valid style attribute in JSX?
3. What can go inside braces in JSX?
4. What does a fragment <>...</> add to the page?

Key takeaways

  • Braces put any JavaScript expression into JSX: values, calls, ternaries.
  • Attributes follow JavaScript names: className, htmlFor, onClick, and style takes an object.
  • A component returns one element; a fragment groups several without extra markup.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.