Skip to content

JSX Basics

JSX syntax rules, embedding expressions, and how JSX compiles to React.createElement calls.

What you'll learn

  • What JSX is and why it looks like HTML but is JavaScript
  • How to embed JavaScript expressions inside JSX with curly braces
  • The rules of JSX: single root, self-closing tags, className vs class
  • How JSX compiles down to React.createElement

Concept

What Is JSX?

JSX is a syntax extension for JavaScript that lets you write markup that looks like HTML directly inside your JavaScript code. It is not valid JavaScript by itself — a compiler like Babel or the TypeScript compiler transforms it into regular JavaScript function calls. Despite looking like HTML, JSX is fundamentally JavaScript, which means you can use the full power of the language inside it.

Embedding Expressions

You embed any valid JavaScript expression inside JSX using curly braces {}. This includes variables, function calls, arithmetic, and ternary operators. You cannot embed statements like if or for directly — use ternary operators or extract logic into variables or helper functions above the return statement.

JSX Rules

JSX has a few strict rules. First, a component must return a single root element — wrap siblings in a <div>, a Fragment <></>, or an array. Second, all tags must be closed — <img /> not <img>. Third, use className instead of class for CSS classes because class is a reserved word in JavaScript. Fourth, style attributes receive an object, not a string: style={{ color: "red" }}.

How JSX Compiles

Every JSX element is compiled to a React.createElement() call. Writing <h1 className="greeting">Hello</h1> becomes React.createElement("h1", { className: "greeting" }, "Hello"). Understanding this helps you debug errors and appreciate that JSX is just syntactic sugar — there is no magic, only function calls that return objects describing the UI.

JSX vs HTML Differences

While JSX looks like HTML, there are important differences. Attributes use camelCase (onClick not onclick, tabIndex not tabindex). Style is an object (style={{ color: "red" }}) not a string. Boolean attributes like disabled need an explicit value. Comments use {/* comment */} syntax inside JSX. These differences exist because JSX is JavaScript, and the attribute names follow JavaScript conventions.

Example

              // Embedding expressions in JSX with curly braces.
function Profile({ user, isLoggedIn }) {
  const fullName = `${user.first} ${user.last}`;
  const greeting = isLoggedIn ? "Welcome back" : "Please log in";

  return (
    <div className="profile-card">
      <h2>{greeting}, {fullName}!</h2>
      <p>Member since {user.joinedYear} ({new Date().getFullYear() - user.joinedYear} years)</p>
      <img src={user.avatarUrl} alt={fullName} className="avatar" />
    </div>
  );
}
            

Notice className instead of class, self-closing img tag, and curly braces wrapping every JavaScript expression including template literals and arithmetic.

Try it

Common mistakes

The mistake

Using class instead of className for CSS classes

The fix

class is a reserved word in JavaScript. In JSX, always use className to apply CSS classes. Use style={{}} for inline styles.

The mistake

Returning multiple sibling elements without a wrapper

The fix

Wrap them in a single parent <div>, a React Fragment <></>, or return an array with key props. A component must return one root element.

Practice JSX Basics

5 exercises

Practice JSX Basics

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors