Skip to content
Getting StartedChapter 3 / 37Beginner4 min read

JSX

Learn what React JSX is, how HTML-like syntax works inside JavaScript, and the rules for curly braces, className and writing valid JSX.

On this page

1. What is JSX?

JSX looks like HTML, but it is really JavaScript. A build tool (like Vite) turns it into normal JavaScript function calls that create React elements.

jsx
const heading = <h1 className="title">Hello</h1>;

2. Why does it matter?

You can write React without JSX, but you have to write a function call for every element, and nested UI becomes very hard to read.

Without JSXWith JSX
A createElement call for every elementDirect, HTML-like markup
A jungle of brackets when nestingThe structure is clear at a glance
UI and logic in separate filesUI and logic together in the component
Typos are caught at runtimeThe editor and TypeScript tell you right away

3. Where is it used?

PlaceExample
Every React componentThe UI inside return
ListsCreate elements with items.map()
Conditional UIisLoggedIn && <Profile />
Next.js and React NativeThe same JSX syntax
Files.jsx, or .tsx with TypeScript

4. How does it work?

From JSX to the screen
You write JSX
Vite or Babel turns it into jsx function calls
Each call creates a React element object
React builds the DOM from these objects

So these two snippets are equal:

jsx
const a = <h1 className="title">Hello</h1>;
js
import { jsx } from "react/jsx-runtime";

const a = jsx("h1", { className: "title", children: "Hello" });

Thanks to the new JSX transform, you no longer need to write import React from "react" in every file.

JSX rules

RuleExample
Return only one parent element<div>...</div> or a Fragment <>...</>
Close every tag<img />, <br />, <input />
className instead of class<p className="note">
htmlFor instead of for<label htmlFor="email">
Attributes in camelCaseonClick, tabIndex, maxLength
JavaScript inside { }<p>{user.name}</p>
style takes an objectstyle={{ color: "red" }}

5. Code example

jsx
export default function ProfileCard() {
  const user = { name: "Ayesha", role: "Frontend Dev", isOnline: true };
  const skills = ["HTML", "CSS", "React"];

  return (
    <>
      {/* This is a JSX comment */}
      <section className="card">
        <h2>{user.name.toUpperCase()}</h2>
        <p style={{ color: "gray", fontSize: 14 }}>{user.role}</p>
        <p>Status: {user.isOnline ? "Online" : "Offline"}</p>
        <label htmlFor="msg">Message</label>
        <input id="msg" placeholder="Type here..." />
        <ul>
          {skills.map((skill) => (
            <li key={skill}>{skill}</li>
          ))}
        </ul>
      </section>
    </>
  );
}

How does the code work?

ItemMeaning
<>...</>A Fragment, one parent without an extra div
{user.name.toUpperCase()}Any JavaScript expression inside curly braces
style={{ ... }}The outer braces are JavaScript, the inner one is the object
fontSize: 14React turns the number into 14px for you
? :A condition with a ternary, an if statement does not work in JSX
skills.map(...)A list from an array, with a unique key for each item
{/* ... */}How to write a comment inside JSX

Interview tip

"Does the browser understand JSX?" No. JSX is compiled into JavaScript function calls at build time. Only normal JavaScript reaches the browser.

6. Common mistakes

Mistake: Returning two sibling elements without a parent

Why? A function can return only one value, so you get the adjacent JSX elements error.

Better: Wrap both in a div or a Fragment.

Mistake: Writing class and for

Why? These are reserved words in JavaScript, and React shows a warning.

Better: Use className and htmlFor.

Mistake: Writing if or for inside curly braces

Why? Only expressions work in JSX, not statements.

Better: Use a ternary, the && operator or map, or write the logic before the return.

Mistake: Rendering an object directly

Why? React cannot turn an object into text and gives the Objects are not valid as a React child error.

Better: Render a field of the object, like user.name, or use JSON.stringify.

7. What to learn before and after

Final quick revision

PointDetail
JSXHTML-like syntax in JavaScript
CompileReact elements through jsx() calls
Adding JavaScript{ } curly braces
ParentOne root element or a Fragment
AttributesclassName, htmlFor, camelCase
StyleAn object, style={{ }}