Embedding Expressions in JSX
In this page:
function ComponentName() {
const variable = value;
return <element>{variable}</element>; // any JS expression in { }
}
<element>{functionName(argument)}</element>
Embedding JavaScript with Curly Braces
Any JavaScript expression can be inserted into JSX by wrapping it in curly braces {} -- variables, function calls, arithmetic, and more.
Example: Embedding JavaScript with Curly Braces
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function Sum() {
const a = 4;
const b = 5;
return <p>{a} + {b} = {a + b}</p>;
}
ReactDOM.createRoot(document.getElementById('root')).render(<Sum />);
</script>
</body>
</html>
Using Variables Inside JSX
A variable defined in your component function can be referenced directly inside the returned JSX using {variableName}.
Example: Using Variables Inside JSX
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function Greeting() {
const name = 'Asha';
return <p>Hello, {name}!</p>;
}
ReactDOM.createRoot(document.getElementById('root')).render(<Greeting />);
</script>
</body>
</html>
Calling Functions Inside JSX
You can call a function directly inside curly braces, and its return value is inserted into the output -- useful for formatting values right where they're displayed.
Example: Calling Functions Inside JSX
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function formatPrice(cents) {
return '$' + (cents / 100).toFixed(2);
}
function Price() {
return <p>Price: {formatPrice(1999)}</p>;
}
ReactDOM.createRoot(document.getElementById('root')).render(<Price />);
</script>
</body>
</html>
- Trying to embed a JavaScript statement (like an if block) directly inside JSX curly braces -- only expressions work there.
- Forgetting that objects can't be rendered directly as JSX children -- you'll get an error.
- Using curly braces around an entire JSX block instead of just the dynamic value.
- Curly braces
{}embed JavaScript expressions inside JSX. - Any valid expression works: variables, function calls, arithmetic, ternaries.
- Statements like if/for do not work directly inside
{}-- use expressions instead. - Rendering a plain object throws an error; render its properties instead.
No browser-specific restrictions -- this is a JSX/Babel compilation-time feature.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: