Home / Learn React / JSX: Markup Inside JavaScript

JSX: Markup Inside JavaScript

JSX looks like HTML but it lives inside JavaScript. It lets you write the structure of a component right next to the logic that decides what to show.

In shortJSX is a syntax that is turned into JavaScript calls. It follows a few strict rules: one root, close every tag, use camelCase attributes.

Theory in plain words

What happens to JSXJSX you write<h1>Hi</h1>Compiler (Vite,Babel, Sucrase)jsx("h1", ...)plain JavaScriptReact elementobjectReal DOM node
What happens to JSX

What it is

JSX is a syntax extension for JavaScript. A line such as <h1>Hi</h1> is not a string and not real HTML. A compiler changes it into React.createElement("h1", null, "Hi") or the modern jsx() call.

Why we use it

Rendering logic and markup belong together: a button, the code that runs when you click it and the text it shows are all one idea. JSX keeps them in one place and lets you use full JavaScript inside the markup.

How it works

The build tool (or the tiny compiler running on this page) converts JSX into plain JavaScript objects that describe the UI. React reads those objects and creates the real DOM elements.

Where it is used

  • Every React component returns JSX
  • Templates for lists, tables and forms
  • Conditional UI such as logged in and logged out views

Rule 1: return one root element

A component can return only one thing. Wrap siblings in a <div> or in a Fragment <>...</> that adds no extra element to the page.

One root with a FragmentTry it Yourself

Open the browser inspector on the result: there is no wrapper div.

Rule 2: close every tag

HTML lets you write <br> and <img> without closing. JSX does not. Write <br /> and <img />. Also close <input />.

Rule 3: camelCase attributes

HTMLJSXWhy
classclassNameclass is a reserved word in JavaScript
forhtmlForfor is a reserved word
onclickonClickEvents are camelCase and take a function
tabindextabIndexcamelCase
style="color:red"style={{ color: "red" }}style takes an object
className, htmlFor and style objectTry it Yourself

The double braces are not special: the outer pair says "JavaScript here", the inner pair is an object.

Rule 4: use curly braces for JavaScript

Inside JSX, { } opens a window into JavaScript. You can put variables, function calls and expressions in them, both as text and as attribute values. You cannot put statements such as if or for in them. Use ? : and .map() instead.

Expressions in bracesTry it Yourself

Comments and text

Comments in JSX
return (
  <div>
    {/* this is a comment inside JSX */}
    <p>Hello</p>
  </div>
);
Careful:A common bug: {count && <List />} shows a lone 0 when count is 0. Write {count > 0 && <List />} instead.
Fix the bug: why does 0 show?Try it Yourself

Quick quiz

1. Which attribute do you use for a CSS class in JSX?

2. What is wrong with return (<h1>A</h1><p>B</p>)?

3. What does {items.length && <List />} show when items is empty?

4. How do you write an inline style in JSX?

Common questions

Which attribute do you use for a CSS class in JSX?

className. class is a reserved word in JavaScript, so JSX uses className.

What is wrong with return (<h1>A</h1><p>B</p>)?

A component must return one root element. Wrap siblings in a div or a Fragment.

What does {items.length && <List />} show when items is empty?

The number 0. 0 is a number and React renders it. Compare with > 0 instead.