Evaluating Expressions: Math, String Concatenation, Function Calls
What you'll learn: How to use JavaScript expressions inside JSX curly braces to perform calculations, combine strings, and call functions.
Beyond Simple Variables
You already know curly braces {} let you interpolate variables into JSX. But they're far more powerful—they accept any valid JavaScript expression. An expression is code that produces a value: math operations, string concatenation, function calls, and more.
Math Operations
You can perform arithmetic directly inside curly braces:
function Calculator() {
return <div>5 + 3 = {5 + 3}</div>;
// Renders: 5 + 3 = 8
}
The expression 5 + 3 evaluates to 8 before React renders it.
String Concatenation
Combine strings on the fly:
function Greeting() {
const firstName = "Ada";
const lastName = "Lovelace";
return <h1>{"Hello, " + firstName + " " + lastName + "!"}</h1>;
// Renders: Hello, Ada Lovelace!
}
Function Calls
Call functions and use their return values:
function getExcitedGreeting(name) {
return name.toUpperCase() + "!!!";
}
function Welcome() {
return <p>{getExcitedGreeting("world")}</p>;
// Renders: WORLD!!!
}
The function runs, returns a value, and that value gets rendered.
Think of Curly Braces as Mini JavaScript Zones
Imagine JSX as a template where curly braces are windows into JavaScript's calculation engine. Whatever you write inside must produce a single value that React can display.
Key Takeaway: Curly braces in JSX evaluate any JavaScript expression—math, string operations, function calls—and render the resulting value. This makes your UI dynamic without leaving the markup.