Home / React / Lesson 3

JSX — HTML-looking JavaScript

Beginner 8 min read Lesson 3 of 11

JSX is syntax that looks like HTML inside JavaScript. A build tool (Vite + Babel/SWC) turns it into function calls. This:

const el = <h1 className="title">Hello</h1>;

is the same idea as React.createElement('h1', { className: 'title' }, 'Hello'). Browsers do not run JSX without that compile step.

Rules that trip beginners

function Hello({ name, loggedIn }) {
  return (
    <section>
      <h1>Hi, {name}</h1>
      {loggedIn ? <p>Welcome back.</p> : <p>Please log in.</p>}
    </section>
  );
}

JSX is not a string. Putting HTML in a string and assigning it to innerHTML is a different (and XSS-prone) path. Stay in JSX.