- Introduction We’ve covered JSX and rendering elements in React. Now it’s time to dive into Expressions in JSX. JSX allows us to embed JavaScript expressions within our code, making it even more powerful! Let’s explore this topic with the help of our two characters, Geek and Gal.
- Expressions in JSX
const a = 5;
const b = 3;
const element = <h1>The sum of {a} and {b} is {a + b}!</h1>;
- Hilarious Expression Example
const num1 = 7;
const num2 = 2;
const sillyMath = num1 * num2 - (num1 + num2);
const element = (
<h1>
What's {num1} times {num2} minus the sum of {num1} and {num2}? It's{' '}
{sillyMath}! 🤪
</h1>
);
- Conclusion
Embedding expressions in JSX is a powerful feature that allows us to include JavaScript logic within our React components. Just wrap any valid JavaScript expression in curly braces
{}
to use it within your JSX code. Keep exploring and enjoy the flexibility that JSX offers! 😃