Embedding JavaScript Expressions in JSX
Learn how to display variables, calculations, function results, and dynamic values inside JSX using curly braces.
1. What Are JSX Expressions?
JSX allows you to embed JavaScript expressions inside markup
by using curly braces { }.
This makes it possible to display dynamic values instead
of writing fixed text in your components.
An expression is JavaScript code that produces a value. For example, a variable, a mathematical calculation, or a function call that returns a value can be used inside JSX.
Important Rule
Use curly braces inside JSX to insert a JavaScript expression.
function App() {
const name = "Rahul";
return <h1>Hello, {name}!</h1>;
}
export default App;
Expected Browser Output
Hello, Rahul!
2. Using Variables in JSX
Variables store data such as names, course names, numbers, and other values. You can display these values inside JSX using curly braces.
function App() {
const studentName = "Priya";
const course = "React JS";
const city = "Pune";
return (
<div>
<h1>Student Details</h1>
<p>Name: {studentName}</p>
<p>Course: {course}</p>
<p>City: {city}</p>
</div>
);
}
export default App;
Expected Browser Output
Student Details
Name: Priya
Course: React JS
City: Pune
{studentName} to display the variable's
value. Writing "studentName" displays the
literal text instead.
3. Mathematical Expressions in JSX
You can perform mathematical calculations directly inside JSX. JavaScript evaluates the expression and displays the resulting value.
function App() {
const a = 10;
const b = 20;
return (
<div>
<h2>Mathematical Operations</h2>
<p>Addition: {a + b}</p>
<p>Subtraction: {a - b}</p>
<p>Multiplication: {a * b}</p>
<p>Division: {a / b}</p>
<p>Remainder: {a % 3}</p>
</div>
);
}
export default App;
Expected Browser Output
Mathematical Operations
Addition: 30
Subtraction: -10
Multiplication: 200
Division: 0.5
Remainder: 1
4. Using Function Calls in JSX
A JavaScript function that returns a value can be called inside JSX. The returned value is then displayed in the user interface.
function getMessage() {
return "Welcome to React!";
}
function App() {
return (
<div>
<h1>{getMessage()}</h1>
</div>
);
}
export default App;
Expected Browser Output
Welcome to React!
5. Expressions vs Statements
JSX curly braces accept JavaScript expressions, not arbitrary statements. An expression produces a value, whereas a statement performs an action or controls program execution.
| JavaScript Code | Type | Can Be Used Directly in JSX? |
|---|---|---|
name |
Variable expression | Yes |
10 + 20 |
Arithmetic expression | Yes |
getMessage() |
Function call expression | Yes |
isLoggedIn ? "Yes" : "No" |
Conditional expression | Yes |
if (isLoggedIn) { } |
Statement | No |
const total = 100; |
Variable declaration | No |
Correct Approach
function App() {
const isLoggedIn = true;
return (
<div>
<p>
{isLoggedIn ? "Welcome Back!" : "Please Login"}
</p>
</div>
);
}
export default App;
return statement.
6. Boolean, Null and Undefined Values
Not every JavaScript value appears as visible text in JSX.
Boolean values, null, and
undefined are generally not rendered as text
when used as children.
function App() {
const isActive = true;
const emptyValue = null;
return (
<div>
<p>Boolean: {isActive}</p>
<p>Empty value: {emptyValue}</p>
<p>Number: {100}</p>
</div>
);
}
export default App;
Expected Browser Output
Boolean:
Empty value:
Number: 100
function App() {
const isActive = true;
return (
<p>Active: {String(isActive)}</p>
);
}
export default App;
Output: Active: true
7. Common Mistakes
1. Forgetting Curly Braces
Incorrect for a variable
<p>name</p>
Correct
<p>{name}</p>
Without curly braces, JSX displays the word
name instead of the variable's value.
2. Using an if Statement Inside JSX
Incorrect
{if (isActive) { "Active" }}
Correct
{isActive ? "Active" : "Inactive"}
An if statement cannot be inserted directly
into a JSX expression. Use a ternary expression or
calculate the result before returning JSX.
3. Expecting Boolean Values to Display as Text
Not displayed as the text "true"
<p>{true}</p>
Correct
<p>{String(true)}</p>
Convert Boolean values to strings when you want to
display the words true or
false.
8. Practice Questions
Try solving these exercises in your React application before moving to the next topic.
Display Student Information
Create variables for a student's name, age, and course. Display all three values inside JSX.
Perform Calculations
Create two number variables and display their addition, subtraction, multiplication, and division results using JSX expressions.
Display a Dynamic Message
Create a Boolean variable called
isLoggedIn. Display
"Welcome Back!" when it is true and
"Please Login" when it is false.
Calculate a Total
Create variables for price and quantity. Calculate the total amount and display it inside a paragraph using JSX.
9. Quick Recap
-
Use
{ }to embed JavaScript expressions inside JSX. - Variables can display dynamic data in a component.
- Mathematical calculations can be evaluated inside JSX.
- Function calls can display their returned values.
-
Expressions produce values; statements such as
ifcannot be inserted directly into JSX. - Boolean, null, and undefined values are not displayed as ordinary text by default.