Chapter 2: JSX Basics

1. What is JSX actually?

JSX stands for JavaScript XML. It’s a syntax extension for JavaScript that lets you write HTML-like code inside JavaScript.

Important: JSX is NOT HTML — it’s just a nicer way to write JavaScript that eventually gets converted into regular JavaScript function calls.

When you write this:

jsx

Behind the scenes, React (actually Babel) transforms it into this plain JavaScript:

JavaScript

So JSX = syntactic sugar — it makes your code look beautiful and readable.

Analogy: JSX is like writing a love letter in beautiful handwriting instead of typing it in plain boring text. The meaning is the same, but it’s much easier and prettier to read.

2. JSX Syntax Rules (very important — follow these always!)

Here are the golden rules every React developer must follow:

Rule Correct JSX Wrong JSX Explanation
Always return ONE root element <div><h1>Hello</h1><p>World</p></div> <h1>Hello</h1><p>World</p> Use <div>, <Fragment> or <>…</> to wrap multiple elements
All tags must be closed <img src=”logo.png” alt=”logo” /> <img src=”logo.png” alt=”logo”> Self-closing tags are mandatory (even for <br>, <input>)
Attributes use camelCase className, onClick, tabIndex class, onclick, tabindex Because it’s JavaScript, not HTML
JavaScript expressions go inside {} {user.name} user.name Curly braces = escape into JS mode
No quotes around curly braces <p>{2 + 2}</p> <p>”{2 + 2}”</p> Curly braces already mean JS
Comments are special {/* this is a comment */} <!– this won’t work –> Use JS-style comments inside {}
No if, for, while inside JSX Use ternary or && <div>{if (true) …}</div> Use expressions only

Modern best practice (2026): Use Fragments (<></>) instead of unnecessary <div> wrappers:

jsx

3. Embedding Expressions in JSX (this is where the magic happens!)

You can put any valid JavaScript expression inside {}.

jsx

Important: You cannot put statements like if, for, while, or variable declarations inside {} — only expressions that produce a value.

4. JSX vs HTML – Key Differences

Feature HTML JSX (React)
Attribute names class, for, onclick className, htmlFor, onClick
Self-closing tags Optional <br> Mandatory <br />
Comments <!– comment –> {/* comment */}
Inline styles style=”color: red;” style={{ color: “red” }} (object)
Event handlers onclick=”handleClick()” onClick={handleClick} (function ref)
Case sensitivity Not case-sensitive Case-sensitive (components start with Capital letter)
Boolean attributes <input disabled> <input disabled={true} /> or just <input disabled />

Biggest mind-blow: In JSX, your components must start with a Capital letter!

jsx

Your First JSX Exercise (do this right now!)

Replace everything inside the return of App.tsx with this beautiful example:

tsx

Save the file → see your beautiful personalized page update instantly! 🎉

Summary – Chapter 2 Key Takeaways

  • JSX = HTML-like syntax inside JavaScript
  • It gets converted to React.createElement() calls
  • Must follow strict rules: camelCase attributes, closed tags, one root element, {} for JS expressions
  • Use {} to embed any JavaScript expression
  • Components start with Capital letter
  • JSX is not HTML — it’s more powerful!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *