🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·
⚛️ React JS

JSX: HTML-like syntax

📚 What is JSX? JSX is a special syntax that lets you write HTML-like code directly inside JavaScript. React components return JSX, which the build tool converts to regular JavaScript calls. It looks like HTML but has the full power of JavaScript available inside {} expressions.

8 min 10 XP Lesson 5 of 23
JSX: HTML-like syntax
🌐

Appy Says…

JSX looks like HTML inside JavaScript — and that's exactly what it is. React uses JSX so you can describe your UI directly in your component code. It's not magic; it compiles to plain JavaScript function calls. Once you see how, it all makes sense.

📖

What is JSX?

JSX (JavaScript XML) is a syntax extension that lets you write HTML-like code inside JavaScript. Babel transforms it into React.createElement() calls before it runs in the browser.

  • return <div className="card"><h1>{title}</h1></div>;
  • Use { } to embed any JavaScript expression: {2 + 2}, {name.toUpperCase()}
  • HTML attributes differ: classclassName, forhtmlFor
  • Self-closing tags: <img />, <br />, <input />
  • Must return ONE root element (or use a Fragment: <>...</>)
  • Comments: {/* comment */}
🎮

Think of it like a Minecraft command with templating

Imagine a Minecraft command block that uses variables: /say Hello ${playerName}!. JSX works the same — write your structure like HTML, then insert dynamic values with {curly braces}.

⚙️

How It Works

  • 1. You write JSX: const el = <h1>Hello {name}</h1>;
  • 2. Babel compiles it to: const el = React.createElement('h1', null, 'Hello ', name);
  • 3. React takes those element objects and builds the real DOM
  • 4. { expression } evaluates any valid JS and inserts the result
  • 5. Conditional rendering: {isLoggedIn && <UserMenu />}
  • 6. List rendering: {items.map(i => <Li key={i.id}>{i.name}</Li>)}
🌍

Real-World Examples

  • Dynamic greeting: <h1>Welcome, {user.name}!</h1>
  • Conditional: {error && <ErrorBanner message={error} />}
  • List: {songs.map(s => <SongRow key={s.id} song={s} />)}
  • Style: <div style={{ color: 'red', fontSize: '16px' }}> (object with camelCase props)
💡

Key Facts

  • JSX is NOT HTML — it's syntax sugar for React.createElement()
  • In React 17+, you don't need to import React at the top — the transform handles it
  • JSX expressions must return a value — statements like if/for don't work inside {}
  • TypeScript + JSX uses .tsx file extensions
⚠️

Watch Out!

JSX must have ONE root element. Returning two sibling elements causes an error. Use a Fragment <>...</> to wrap without adding an extra DOM node: return <><Title /><Body /></>;

📌

Remember

{ } embeds JS expressions. className not class. Must return one root element — use <></> fragments.

What You Learned

  • JSX: HTML-like syntax in JS; compiles to React.createElement()
  • { } embeds expressions; className/htmlFor differ from HTML; one root element rule
  • Unlocks: dynamic UIs, conditional rendering, list rendering in React

Key Facts

  • JSX is NOT HTML — it's syntax sugar for React.createElement()
  • In React 17+, you don't need to import React at the top — the transform handles it
  • JSX expressions must return a value — statements like if/for don't work inside {}
  • TypeScript + JSX uses .tsx file extensions

Real-World Examples

• Dynamic greeting: <code>&lt;h1&gt;Welcome, {user.name}!&lt;/h1&gt;</code> • Conditional: <code>{error && &lt;ErrorBanner message={error} /&gt;}</code> • List: <code>{songs.map(s => &lt;SongRow key={s.id} song={s} /&gt;)}</code> • Style: <code>&lt;div style={{ color: 'red', fontSize: '16px' }}&gt;</code> (object with camelCase props)

Remember

{ } embeds JS expressions. className not class. Must return one root element — use <></> fragments.

Quick Quiz

1 / 2

JSX is?