AI Hints
React · Lesson 6 of 120

JavaScript in JSX

Curly braces.

⏱ 2 min read▶ Video at 4:17🎓 Free
Watch this lesson in the React courseWatch on YouTube ↗Full React course

Curly braces open a window from JSX into JavaScript. Inside them you can put any expression. A variable, a calculation, or a function call.

  • Curly braces
  • Any expression
  • Variables, maths, calls

Values in the page

We keep the bike's details in variables. Curly braces put them into the page. We even calculate the price with VAT, and format it. Run it.

App.jsxjsx
const model = "Trail 500";
const price = 849.5;
const inStock = 12;

export default function App() {
  return (
    <div className="box">
      <h2>{model}</h2>
      <p>Price: £{price.toFixed(2)}</p>
      <p>With VAT: £{(price * 1.2).toFixed(2)}</p>
      <p>{inStock} in stock</p>
    </div>
  );
}
Also uses style.css
body{font-family:-apple-system,Helvetica,Arial,sans-serif;margin:22px;font-size:21px;color:#1d1d1f;background:#fafafa}
h1{font-size:34px;margin:0 0 14px} h2{font-size:26px;margin:0 0 10px}
button{font-size:19px;padding:9px 18px;border-radius:9px;border:0;background:#61dafb;color:#111;font-weight:600;margin:4px 6px 4px 0}
input{font-size:19px;padding:8px 10px;border:2px solid #bbb;border-radius:8px}
li{margin:5px 0} .box{padding:14px;border-radius:12px;background:#fff;border:2px solid #ddd;margin:8px 0}
.done{text-decoration:line-through;color:#999} small{color:crimson;display:block;font-size:16px} .big{font-size:64px;font-weight:800}

What fits in braces

Expressions fit, things that produce a value. Variables, sums, function calls, and ternaries. Statements, like if and for, don't fit. We'll see how to handle those soon.

keyvalue
Yesvariables, maths
Yesfunction calls
Yesa ? b : c
Noif, for statements

Summary

Curly braces put JavaScript expressions into JSX. Next, components.

Key takeaways
  • {model}
  • {price * 1.2}
  • Expressions only
Next up: Components — small reusable pieces