Course contentsShow
← React
Lesson 220 of 2,3967. Props FundamentalsFree lesson

Passing Variables and Expressions

Any JavaScript expression can be a prop value: variables, function calls, ternaries, or complex computations.

Passing Variables and Expressions

What you'll learn: How to pass any JavaScript expression as a prop value, not just static strings.

Beyond Static Values

You've learned that props can be strings (like title="Hello"), but the real power comes from using curly braces to pass dynamic JavaScript expressions. Anything that evaluates to a value can become a prop.

Think of props as function arguments. Just as you can pass calculateTotal(items) or isValid ? "yes" : "no" to a function, you can pass the same expressions as props.

What Counts as an Expression?

Any of these can be prop values:

  • Variables: <Card title={userName} />
  • Function calls: <Card count={getTotal()} />
  • Ternaries: <Card status={isActive ? "online" : "offline"} />
  • Math operations: <Card width={baseWidth * 2} />
  • Object properties: <Card name={user.firstName} />
  • Complex computations: <Card average={scores.reduce((a, b) => a + b) / scores.length} />

The key rule: if it's valid JavaScript that produces a value, it can be a prop.

Why This Matters

This lets your components respond to real data instead of hardcoded strings. Your UI becomes truly dynamic—the same component renders differently based on the values you pass.

<UserCard 
  name={currentUser.name}
  age={calculateAge(currentUser.birthdate)}
  badge={currentUser.points > 100 ? "gold" : "silver"}
  isOnline={currentUser.lastSeen > Date.now() - 300000}
/>

Key Takeaway: Curly braces in JSX unlock full JavaScript expressions as prop values—variables, calculations, function calls, and conditionals all work seamlessly, making your components truly data-driven.