Demystifying JSX: Syntax and Expressions in React

Learn the fundamentals of JSX, the HTML-like syntax used in React to describe UI structure. Discover how to embed JavaScript expressions, work with attributes like className, and follow the core rules for writing clean, effective JSX code.

Lesson ProgressStep 1 of 9

Hello, World!

0 EXP

Welcome! Let's explore JSX, the special syntax for building React UIs.

// Welcome to React + JSX

What is JSX?

JSX stands for JavaScript XML. It's a syntax extension that lets you write HTML-like markup inside a JavaScript file. It makes writing React components intuitive and declarative.

Although it looks like HTML, it's not. Your browser can't read JSX. It gets compiled (by a tool like Babel) into regular JavaScript function calls, specifically React.createElement().

// This JSX:
<div className="App">Hello</div>

// Becomes this JavaScript:
React.createElement('div', {className: 'App'}, 'Hello');

System Check

What does JSX stand for?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

✍️
JSX Syntax Pro

Write a valid JSX element with a className attribute.

💡
Expression Master

Correctly embed a JavaScript variable in JSX.

🏗️
Component Builder

Build a multi-element component using a Fragment.

Mission: Build a Profile Card

Inside the component, return a `div` element with a `className` attribute set to `"card"`. Inside that `div`, add an `h1` with your name.

A.D.A. Feedback:

> System integrity looks stable. Code is valid.

Challenge: Build a Component

A component needs to return two elements, but must still follow the "single root" rule. Drag the lines of code into the correct order.

<h1>My App</h1>
<>
</>
<p>Welcome to the app.</p>

Challenge: Complete the Syntax

Fill in the missing parts to correctly assign a `className` and display a JavaScript variable.

const user = { name: "Alice" };return (
<div className=>
<h2>Hello, !</h2>
</div>);

Consult A.D.A.

Community Holo-Net

Peer Project Review

Submit your "Profile Card" component for feedback from other Net-Runners.

JSX in Practice: From Syntax to UI

JSX (JavaScript XML) is the syntax that makes React declarative and easy to read. It looks like HTML, but it's actually a powerful JavaScript abstraction. Understanding how to use it effectively is the most crucial skill for any React developer. Let's dive deep into how JSX works in real-world scenarios.

1. JSX vs. `React.createElement`

Behind the scenes, JSX is just "syntactic sugar" for a standard JavaScript function call: `React.createElement()`. A tool called Babel compiles your JSX into this function.

✔️ You Write This (JSX)

const element = (
  <h1 className="greeting">
    Hello, world
  </h1>
);

🤖 Babel Compiles This

const element = React.createElement(
  'h1',
  {className: 'greeting'},
  'Hello, world'
);

Knowing this relationship is key. It clarifies that JSX is not HTML; it's a way to create JavaScript objects that React uses to build the DOM.

2. Embedding Expressions: The Power of ``

The curly braces `` are your escape hatch back into JavaScript. You can put any valid JavaScript expression inside them.

  • Variables: <h1>Hello, {user.name}</h1>
  • Math: <p>Total: {2 + 2}</p>
  • Function Calls: <div>{formatDate(user.joined)}</div>
  • Ternary Operators: {isLoading ? <Spinner /> : <Content />}
  • Logical `&&` Operator: {showWarning && <Warning />}

Important: You cannot put statements (like `if`, `for`, `while`) inside ``. You must use expressions that resolve to a value.

3. Specifying Attributes

JSX attributes are similar to HTML but follow JavaScript's `camelCase` naming convention.

  • `className` (not `class`): <div className="card">...</div>
  • `htmlFor` (not `for`): <label htmlFor="email">Email</label>
  • Event Handlers: <button onClick={handleClick}>Click Me</button>
  • Style (Double Braces): The `style` attribute takes a JavaScript object. This results in double braces: one for the JSX expression, and one for the object literal.
    <div style={{ color: 'red', fontSize: '16px' }}>...</div>
  • Boolean Attributes: You can pass `true` explicitly (disabled={true}) or just by including the prop (disabled). To pass `false`, you must use braces (disabled={false}).

4. The Single Root Element Rule & Fragments

A React component must return a single root element. React's diffing algorithm needs one "root" to check against.

❌ Bad Practice (Error)

return (
  <h1>Title</h1>
  <p>Text</p>
);

This will cause a syntax error.

✔️ Good Practice

return (
  <>
    <h1>Title</h1>
    <p>Text</p>
  </>
);

Wrapped in a Fragment (`...`).

You can either wrap your elements in a `div` (which adds an extra node to the DOM) or use a Fragment (<React.Fragment>...</React.Fragment> or the shorthand <>...</>) which doesn't render any extra HTML.

5. Rendering Lists with `.map()` and `key`

You'll often need to render a list of items. The standard way is to use the `.map()` array method inside an expression block. When you do this, React requires a unique `key` prop for each item to help it identify which items have changed, been added, or removed.

const todos = [
  { id: 'a', text: 'Learn JSX' },
  { id: 'b', text: 'Build App' },
];

return (
  <ul>
    {todos.map(todo => (
      <li key={todo.id}>
        {todo.text}
      </li>
    ))}
  </ul>
);

6. JSX and Security (XSS)

By default, React DOM **escapes** any values embedded in JSX before rendering them. This means you are protected from Cross-Site-Scripting (XSS) attacks.

If `user.name` is <script>alert('hack')</script>, React will not execute the script. It will render the literal text on the screen, foiling the attack.

Key Takeaway: JSX is the heart of React. Master its rules: use `{}` for expressions, `className` for styling, and `<>` for grouping, and you'll be able to build any UI you can imagine.

React JSX Glossary

JSX (JavaScript XML)
A syntax extension for JavaScript that lets you write HTML-like markup inside a JavaScript file. It is compiled by tools like Babel into `React.createElement()` calls.
Babel
A JavaScript compiler that transforms modern JavaScript (ES6+) and syntax extensions (like JSX) into backward-compatible JavaScript that browsers can understand.
Element
A React Element is a light, stateless, immutable JavaScript object that describes what you want to see on the screen. `

Hello

` in JSX compiles into an element.
Expression: ``
The "escape hatch" in JSX. Curly braces allow you to embed any valid JavaScript expression (a variable, a function call, a ternary) directly into your markup.
camelCase
The naming convention used for JSX attributes that correspond to multi-word HTML attributes (e.g., `onclick` becomes `onClick`, `tabindex` becomes `tabIndex`).
`className`
The JSX attribute for specifying CSS classes. It's named this way because `class` is a reserved keyword in JavaScript.
`htmlFor`
The JSX attribute for the HTML `for` attribute on `<label>` tags. `for` is a reserved keyword for loops in JavaScript.
`style` (Double Braces)
The JSX attribute for inline styles. It accepts a JavaScript object with camelCased style properties: `style={{ color: 'red' }}`.
Root Element (Single)
The rule that a React component's `return` statement must only have one top-level element.
Fragment: `<>` or `<React.Fragment>`
A special component used to wrap multiple elements without adding an extra, unnecessary node (like a `div`) to the DOM.
`key` prop
A special string attribute you need to include when creating lists of elements (e.g., with `.map()`). Keys help React identify which items have changed, are added, or are removed.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching React and building robust and accessible web applications.

Verification and Updates

Last reviewed: November 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest React documentation and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!