Math in Braces
Part of the Fundamentals section of Coddy's React journey — lesson 7 of 42.
The braces don't just hold variables: any JavaScript expression works. Math, string methods, function calls:
const price = 6;
const quantity = 5;
return <p>Total: {price * quantity}</p>;
// renders: Total: 30React evaluates the expression and renders the result. You can combine text and braces freely in one element:
<p>{quantity} items at {price} each</p>One thing that does NOT belong in braces: statements. if, for and friends aren't expressions. We'll meet their JSX-friendly replacements in a later chapter.
Challenge
BeginnerBuild a tiny receipt. The component defines price and quantity.
- Make the paragraph with
id="total"renderTotal: 30, but compute the number from the variables with*, don't type 30.
Try it yourself
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div id="root"></div>
<script type="module" src="main.jsx"></script>
</body>
</html>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
2JSX Expressions
Curly BracesMath in BracesBraces in AttributesCalling Functions in JSXRecap: JSX Expressions5State & Events
What Is State?Click CounterCount in StepsToggle with StateIndependent StateRecap: State & Events8Project: Profile Card
Step 1: Card StructureStep 2: Card with PropsStep 3: Like ButtonStep 4: Follow Toggle