← Learning Hub
Beginner

JSX

JSX lets you write HTML-like code inside JavaScript. It looks like HTML, but it is turned into JavaScript before it runs.

Example
const heading = <h2>Today's deals</h2>;

Without JSX, you'd create elements with function calls like React.createElement('h2', null, "Today's deals"), which is hard to read for anything bigger. JSX lets you write what looks like HTML, so your components show at a glance what they'll render. A tool (Babel or Vite) converts JSX into those function calls before the browser runs it.

Because JSX is JavaScript, you can store it in variables, return it from functions and build it with loops and conditions - something plain HTML can't do.

Put JavaScript inside JSX with { }

Inside curly braces { } you can put any JavaScript value: a variable, a sum or a function call.

Example
<!DOCTYPE html>
<html>
  <body>
    <div id="root"></div>

    <script src="https://unpkg.com/react@18.3.1/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@18.3.1/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/@babel/standalone@7.24.7/babel.min.js"></script>

    <script type="text/babel">
      function App() {
        const shop = "Terixto";
        const price = 499;
        return (
          <div>
            <h2>Welcome to {shop}</h2>
            <p>Price with 10% off: Rs {price * 0.9}</p>
          </div>
        );
      }

      ReactDOM.createRoot(document.getElementById("root")).render(<App />);
    </script>
  </body>
</html>
▶ Try it yourself

The curly braces are the "window" between JSX and JavaScript. Anything that produces a value can go inside: {price * qty}, {user.name}, {formatDate(today)}. Statements like if or for can't go directly inside the braces - you'll see the JSX way to do conditions and lists in a later lesson.

Three JSX rules

  • Return one outer element. Wrap many elements in a <div> or an empty <>...</>.
  • Close every tag, even empty ones: <img />, <br />.
  • Use className instead of class.
Key points
  • JSX is HTML-like syntax inside JavaScript.
  • Put any JavaScript expression inside { }.
  • One outer element, close every tag, use className.

📝 Test yourself

1. What does JSX let you do?
2. Which attribute gives an element a CSS class in JSX?
3. Fill in the characters that put a JavaScript value inside JSX:
<p>price}</p>