← React tutorial

React

JSX

JSX is the syntax that lets you write HTML-like markup inside a JavaScript function. It isn't a string, and it isn't real HTML either, it compiles down to regular JavaScript function calls that build up the UI piece by piece.

Example: mixing markup and real JavaScript

function Greeting() {
  const name = "Ada";
  const hour = 14;
  return (
    <div>
      <p>Hello, {name}!</p>
      <p>2 + 2 is {2 + 2}.</p>
      <p>{hour < 18 ? "Good afternoon" : "Good evening"}</p>
    </div>
  );
}

Curly braces { } are the escape hatch back into real JavaScript, anything inside them is evaluated as an expression, not printed as literal text. A variable, a calculation, even a ternary expression like the third line above all work exactly the way they would in ordinary JavaScript, just embedded directly inside markup. This is JSX's whole value: markup and the logic that drives it live in the same place, instead of split across a template file and a separate script.

Example
function Greeting() {
  const name = "Ada";
  return <p>Hello, {name}! 2 + 2 is {2 + 2}.</p>;
}
Output
Renders: Hello, Ada! 2 + 2 is 4.
Ready to go further?

These free tutorials are a preview, our Frontend Development cohort covers it in depth, with a real project and a cohort alongside you. Closes Nov 30

Enroll in Frontend Development

Cookie preferences

Choose what you're comfortable with. You can change this any time from the footer.

Don't miss out

Ready to build something real?

Create a free account to enroll in a track, and get notified the moment a cohort opens.

Create free account