Aral Code

Getting Started Lesson 3 of 13

React JSX — Writing HTML in JavaScript

Learn JSX rules — one parent element, fragments, className, self-closing tags, camelCase attributes, inline styles, and embedding JavaScript expressions with curly braces.

JSX lets you write markup that looks like HTML right inside JavaScript. It's not a string and not real HTML: it's turned into JavaScript function calls that create React elements. A few rules make it different from HTML.

Expressions in curly braces

Example Embedding JavaScript in JSX
<div id="root"></div>
<script src="/vendor/react.min.js"></script>
<script src="/vendor/react-dom.min.js"></script>
<script src="/vendor/babel.min.js"></script>
<script type="text/babel">
  const student = { first: "Maria", last: "Clara", grades: [92, 88, 95] };
  const average = student.grades.reduce((a, b) => a + b, 0) / student.grades.length;

  function App() {
    return (
      <div style={{ fontFamily: "sans-serif", padding: 16 }}>
        <h2>{student.first} {student.last}</h2>
        <p>Average: {average.toFixed(2)}</p>
        <p>Status: {average >= 90 ? "With honors 🏅" : "Passed"}</p>
        <p>Next year: Grade {10 + 1}</p>
      </div>
    );
  }
  ReactDOM.createRoot(document.getElementById("root")).render(<App />);
</script>

Anything inside { } is a JavaScript expression: variables, math, function calls, the ternary operator. Statements like if and for can't go inside braces directly (you'll see alternatives in later lessons).

JSX rules

RuleHTMLJSX
Class attributeclass="box"className="box"
Label's forfor="name"htmlFor="name"
Self-closing tags<img>, <br><img />, <br /> (must close)
Event namesonclickonClick (camelCase)
Inline stylestyle="color: red"style={{ color: "red" }}
Comments<!-- -->{/* comment */}

One parent element

A component must return a single root element. Wrap siblings in a <div> or, to avoid an extra element, a fragment <>...</>:

function Header() {
  return (
    <>
      <h1>Aral Code</h1>
      <p>Free coding lessons</p>
    </>
  );
}

Styles and classes

Example className and inline styles
<div id="root"></div>
<script src="/vendor/react.min.js"></script>
<script src="/vendor/react-dom.min.js"></script>
<script src="/vendor/babel.min.js"></script>
<style>
  .badge { display: inline-block; padding: 4px 10px; border-radius: 99px; color: white; font: 14px sans-serif; }
  .ok { background: #1d7a43; }
</style>
<script type="text/babel">
  function App() {
    const stock = 0;
    const warning = { color: "crimson", fontWeight: "bold", fontFamily: "sans-serif" };
    return (
      <div style={{ padding: 16 }}>
        {/* className instead of class */}
        <span className="badge ok">In stock</span>
        <p style={warning}>{stock === 0 && "Chips: sold out"}</p>
        <img src="/img/mango.svg" alt="Mango" width="80" />
      </div>
    );
  }
  ReactDOM.createRoot(document.getElementById("root")).render(<App />);
</script>

The style attribute takes a JavaScript object, hence the double braces: the outer braces mean "JavaScript here", the inner ones create the object. CSS property names become camelCase: fontWeight, backgroundColor.

JSX is safe by default

Text you put in braces is automatically escaped. If a user's name contains <script>, React shows it as plain text instead of running it. This protects against XSS without you doing anything.

What JSX becomes

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

// What it compiles to
const el2 = React.createElement("h1", { className: "title" }, "Hello");

That's why JSX needs React (or a build tool) to work: browsers don't understand JSX on their own.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. How do you set a CSS class in JSX?
2. What goes inside { } in JSX?
3. How do you return two sibling elements without an extra div?
4. How is an inline style written in JSX?

By Sir Reyel · Last updated 2026-12-04