Passing Strings and Numbers in React
Course: React JS Training
Topic: Passing Strings and Numbers as Props in React
Objective: Understand how to pass string and number values from a parent component to a child component using JSX and props, access those values, display them, and use them in calculations and dynamic UI rendering.
1. Introduction to Passing Strings and Numbers
React allows components to communicate with each other using a feature called props. Props are used to pass information from a parent component to a child component. The information can include strings, numbers, booleans, arrays, objects, functions, and other JavaScript values.
Strings and numbers are two of the most commonly used data types in React applications. Strings are useful for names, titles, descriptions, labels, and messages. Numbers are useful for prices, quantities, ages, marks, ratings, counts, and other numerical information.
For example, a parent component can pass a student's name as a string and their marks as a number to a child component.
function Student(props) {
return (
<div>
<h2>Name: {props.name}</h2>
<p>Marks: {props.marks}</p>
</div>
);
}
function App() {
return <Student name="Rahul" marks={85} />;
}
export default App;
In this example, name receives the string value "Rahul", while marks receives the number value 85. The child component accesses both values through its props object.
2. What Is a String in React?
A string is a data type used to represent text. In JavaScript, strings are usually written inside single quotes, double quotes, or backticks. In JSX, a string prop can be passed directly by placing its value inside quotation marks.
Examples of string values
name="Amit"
city="Mumbai"
course="React JS"
message="Welcome to React"
title="Frontend Developer"
Strings can represent user names, course names, product titles, descriptions, button labels, headings, and other text-based information.
Passing a string prop
function Welcome(props) {
return <h1>Welcome, {props.name}!</h1>;
}
function App() {
return <Welcome name="Amit" />;
}
export default App;
Output: Welcome, Amit!
The parent component passes the string "Amit" through the name prop. The child component receives that value and displays it inside an h1 element.
3. What Is a Number in React?
A number is a JavaScript data type used to represent numerical values. React components commonly receive numbers through props to display or process values such as prices, ages, quantities, scores, percentages, and product ratings.
Examples of number values
age={22}
marks={90}
price={499}
quantity={5}
rating={4.5}
discount={10}
When passing a number in JSX, use curly braces around the numeric expression. This allows React to receive the value as a JavaScript number rather than as text.
Passing a number prop
function Student(props) {
return <p>Marks: {props.marks}</p>;
}
function App() {
return <Student marks={90} />;
}
export default App;
Output: Marks: 90
Here, marks={90} passes the numeric value 90 to the child component.
4. Difference Between Passing Strings and Numbers
| Feature |
String |
Number |
| Meaning |
Represents text |
Represents a numerical value |
| JSX syntax |
name="Amit" |
age={22} |
| Data type |
string |
number |
| Typical uses |
Names, titles, messages |
Prices, ages, marks, quantities |
| Arithmetic |
Usually requires conversion if used as a numeric value |
Can be used directly in arithmetic |
| Example output |
React JS |
250 |
Important: price="500" passes a string, while price={500} passes a number. They may look similar when displayed, but their JavaScript data types are different.
5. Passing Strings to Child Components
A parent component passes a string by specifying a prop name and placing the text inside quotation marks. The child component receives the string through its props parameter.
Example 1: Passing a name
function User(props) {
return <h2>User Name: {props.name}</h2>;
}
function App() {
return <User name="Priya" />;
}
export default App;
Output: User Name: Priya
Example 2: Passing a course title
function Course(props) {
return (
<div>
<h2>{props.title}</h2>
<p>Learn the fundamentals of this course.</p>
</div>
);
}
function App() {
return <Course title="React JS Training" />;
}
export default App;
Output: The heading displays React JS Training, followed by the course description.
Example 3: Passing multiple strings
function Profile(props) {
return (
<div>
<h2>{props.name}</h2>
<p>City: {props.city}</p>
<p>Profession: {props.profession}</p>
</div>
);
}
function App() {
return (
<Profile
name="Neha"
city="Mumbai"
profession="Web Developer"
/>
);
}
export default App;
Output:
Neha
City: Mumbai
Profession: Web Developer
Multiple string props allow a component to display different information without repeating the component's entire structure.
6. Passing Numbers to Child Components
Number props allow a child component to display numerical information or perform calculations. Put numeric values or JavaScript expressions inside curly braces.
Example 1: Passing age
function Person(props) {
return <p>Age: {props.age}</p>;
}
function App() {
return <Person age={25} />;
}
export default App;
Output: Age: 25
Example 2: Passing product price
function Product(props) {
return <h2>Price: ₹{props.price}</h2>;
}
function App() {
return <Product price={799} />;
}
export default App;
Output: Price: ₹799
Example 3: Passing multiple numbers
function Result(props) {
return (
<div>
<p>Maths: {props.maths}</p>
<p>Science: {props.science}</p>
<p>English: {props.english}</p>
</div>
);
}
function App() {
return (
<Result maths={85} science={90} english={88} />
);
}
export default App;
Output:
Maths: 85
Science: 90
English: 88
7. Using Curly Braces to Pass JavaScript Values
Curly braces in JSX allow you to use JavaScript expressions. They are essential when passing numbers, variables, calculations, or other JavaScript values as props.
Passing a string variable
function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}
function App() {
const userName = "Rohan";
return <Greeting name={userName} />;
}
export default App;
The userName variable contains a string. The expression name={userName} passes its value to the child component.
Passing a number variable
function Price(props) {
return <p>Product Price: ₹{props.amount}</p>;
}
function App() {
const productPrice = 1200;
return <Price amount={productPrice} />;
}
export default App;
Passing a calculated number
function Total(props) {
return <h2>Total: ₹{props.total}</h2>;
}
function App() {
const price = 200;
const quantity = 3;
return <Total total={price * quantity} />;
}
export default App;
Output: Total: ₹600
Expressions inside curly braces are evaluated by JavaScript before the resulting value is passed to the component.
8. Passing Numbers Versus Numeric Strings
One of the most important concepts is the difference between a number and a string containing digits. A string such as "100" is text, whereas 100 is a numeric value.
function CheckTypes(props) {
return (
<div>
<p>First value: {props.first}</p>
<p>First type: {typeof props.first}</p>
<p>Second value: {props.second}</p>
<p>Second type: {typeof props.second}</p>
</div>
);
}
function App() {
return (
<CheckTypes first="100" second={100} />
);
}
export default App;
Output:
First value: 100
First type: string
Second value: 100
Second type: number
Although both values display as 100, their types are different. This difference matters when performing calculations, comparing values, validating input, or working with APIs.
Example: Adding a number and a string
const a = 10;
const b = "20";
console.log(a + b); // "1020"
console.log(a + Number(b)); // 30
The plus operator can concatenate a string and a number. If you intend to perform arithmetic, convert the string to a number using Number() after validating that the input is suitable.
9. Accessing Props Using Destructuring
React components can access props through the complete props object or by using JavaScript destructuring. Destructuring is a concise way to extract individual values.
Using the props object
function Product(props) {
return (
<div>
<h2>{props.name}</h2>
<p>Price: ₹{props.price}</p>
</div>
);
}
Using destructuring
function Product({ name, price }) {
return (
<div>
<h2>{name}</h2>
<p>Price: ₹{price}</p>
</div>
);
}
function App() {
return <Product name="Keyboard" price={1499} />;
}
export default App;
Both approaches work. Destructuring is often preferred when a component uses only a few named props because it keeps the JSX concise and readable.
10. Using Strings and Numbers Together
Real applications often combine strings and numbers in the same component. For example, a product card may contain a product name, price, and quantity. A student card may contain a name, age, and marks.
Example: Product information card
function ProductCard({ name, price, quantity }) {
return (
<div>
<h2>{name}</h2>
<p>Price: ₹{price}</p>
<p>Quantity: {quantity}</p>
<p>Total: ₹{price * quantity}</p>
</div>
);
}
function App() {
return (
<ProductCard
name="Wireless Mouse"
price={650}
quantity={2}
/>
);
}
export default App;
Output:
Wireless Mouse
Price: ₹650
Quantity: 2
Total: ₹1300
This example demonstrates how a string prop can display a product name while numeric props can be used for calculations.
Example: Student information card
function StudentCard({ name, age, marks }) {
return (
<div>
<h2>Student: {name}</h2>
<p>Age: {age}</p>
<p>Marks: {marks}/100</p>
</div>
);
}
function App() {
return (
<StudentCard name="Anjali" age={21} marks={92} />
);
}
export default App;
11. Passing Values to Multiple Instances of the Same Component
One major advantage of props is that the same component can be reused with different string and number values. This reduces duplicate code and makes applications easier to maintain.
function CourseCard({ title, duration, fee }) {
return (
<div>
<h2>{title}</h2>
<p>Duration: {duration} months</p>
<p>Course Fee: ₹{fee}</p>
</div>
);
}
function App() {
return (
<div>
<CourseCard title="React JS" duration={3} fee={12000} />
<CourseCard title="JavaScript" duration={2} fee={8000} />
<CourseCard title="HTML and CSS" duration={1} fee={5000} />
</div>
);
}
export default App;
Each instance receives different values, but all instances share the same component structure and rendering logic.
12. Performing Calculations with Number Props
Number props are useful when a component needs to calculate totals, averages, discounts, or other derived values.
Example: Calculating a total price
function Bill({ price, quantity }) {
const total = price * quantity;
return <p>Bill Total: ₹{total}</p>;
}
function App() {
return <Bill price={250} quantity={4} />;
}
export default App;
Output: Bill Total: ₹1000
Example: Calculating an average score
function AverageMarks({ maths, science, english }) {
const average = (maths + science + english) / 3;
return <p>Average Marks: {average.toFixed(2)}</p>;
}
function App() {
return (
<AverageMarks maths={80} science={90} english={85} />
);
}
export default App;
Output: Average Marks: 85.00
The toFixed(2) method formats the average to two decimal places and returns a string. It is useful for displaying formatted numbers.
13. Passing String and Number Props with Default Values
A component can provide default values for props when the parent does not supply a value. With function components, default values can be specified in the destructured parameter.
function Profile({ name = "Guest", age = 18 }) {
return (
<div>
<p>Name: {name}</p>
<p>Age: {age}</p>
</div>
);
}
function App() {
return (
<div>
<Profile />
<Profile name="Karan" age={24} />
</div>
);
}
export default App;
The first profile uses the default name and age. The second profile uses the values supplied by the parent.
Important: These destructuring defaults apply when a prop is missing or its value is undefined. Passing null does not trigger the default.
14. Common Mistakes When Passing Strings and Numbers
Mistake 1: Passing a number as a string unintentionally
// Numeric string
<Product price="500" />
// Number
<Product price={500} />
Use the second form when the prop is intended to be numeric.
Mistake 2: Forgetting curly braces for JavaScript variables
const price = 500;
// Incorrect: passes the literal text "price"
<Product price="price" />
// Correct: passes the variable's value
<Product price={price} />
Mistake 3: Using arithmetic on strings
const quantity = "3";
const price = 100;
console.log(price * quantity); // 300 through numeric coercion
console.log(price + quantity); // "1003"
Do not rely on implicit type conversion. Validate and convert data explicitly when arithmetic is required.
Mistake 4: Trying to modify props
function Product({ price }) {
// Do not assign a new value to a prop
// price = 100;
return <p>Price: ₹{price}</p>;
}
Props are read-only inputs to a component. If a value needs to change in response to user interaction, use state or ask the parent component to provide an updated prop.
Mistake 5: Displaying a JavaScript variable without curly braces
const name = "Meera";
// Incorrect: displays the word "name"
<h2>name</h2>
// Correct: displays the variable's value
<h2>{name}</h2>
15. Best Practices for Passing Strings and Numbers
- Use quotation marks for literal string props, such as
name="Rahul".
- Use curly braces for number props, such as
age={25}.
- Use curly braces to pass JavaScript variables and expressions.
- Choose clear prop names such as
title, price, quantity, and marks.
- Use numeric values for calculations instead of numeric strings.
- Validate values received from APIs, forms, or other external sources before using them in calculations.
- Use destructuring when it makes component code easier to read.
- Keep components reusable by passing different values instead of hardcoding all content.
- Do not mutate props inside a component.
- Use sensible default values when a prop is optional.
- Format values for display where appropriate, such as currency or decimal places.
16. Practical Project: Course Information Cards
Project objective: Create reusable course cards that display a course name, duration, fee, and number of modules using string and number props.
App.jsx
function CourseCard({ title, duration, fee, modules }) {
return (
<div className="course-card">
<h2>{title}</h2>
<p>Duration: {duration} months</p>
<p>Course Fee: ₹{fee.toLocaleString("en-IN")}</p>
<p>Modules: {modules}</p>
</div>
);
}
export default function App() {
return (
<main>
<h1>Available Courses</h1>
<CourseCard
title="React JS Training"
duration={3}
fee={15000}
modules={12}
/>
<CourseCard
title="JavaScript Fundamentals"
duration={2}
fee={9000}
modules={8}
/>
<CourseCard
title="Frontend Development"
duration={4}
fee={20000}
modules={16}
/>
</main>
);
}
App.css
body {
margin: 0;
padding: 24px;
font-family: Arial, sans-serif;
background: #f4f6f8;
}
.course-card {
max-width: 420px;
margin: 16px 0;
padding: 20px;
border: 1px solid #ddd;
border-radius: 10px;
background: white;
}
.course-card h2 {
margin-top: 0;
}
Expected result
The page displays three course cards. Each card has a different course title, duration, fee, and module count. The same CourseCard component renders all three courses using the values received through props.
Project learning outcomes
- Pass strings and numbers from a parent component to a child component.
- Use multiple props in a reusable component.
- Format numerical values for display.
- Organize content into reusable UI components.
- Understand the difference between prop values and hardcoded content.
17. Practice Exercises
- Create a
Welcome component that accepts a person's name as a string prop.
- Create a
Product component that accepts a product name and price.
- Create a
Student component that accepts a name, age, and marks.
- Create a
Rectangle component that accepts width and height as numbers and displays the area.
- Create a
ShoppingItem component that calculates the total using price and quantity.
- Create a
CourseCard component that accepts a course title, fee, and duration.
- Use
typeof to check whether a prop is a string or a number.
- Pass a variable containing a string and another variable containing a number to the same component.
- Set default values for an optional name and age prop.
- Create three instances of the same component with different prop values.
18. Interview Questions and Answers
Q1. What are props in React?
Answer: Props are inputs passed to a React component, commonly from a parent component to a child component. They allow components to receive data and render different content.
Q2. How do you pass a string to a component?
Answer: Use quotation marks in JSX, for example <User name="Amit" />.
Q3. How do you pass a number to a component?
Answer: Use curly braces around the numeric value, for example <User age={25} />.
Q4. What is the difference between price="500" and price={500}?
Answer: The first passes a string, while the second passes a number.
Q5. How can a child component access props?
Answer: It can access them through the props object, such as props.name, or by destructuring, such as function User({ name }).
Q6. Can a component receive multiple props?
Answer: Yes. A component can receive multiple string, number, and other JavaScript values through separate props.
Q7. Can number props be used in calculations?
Answer: Yes. Numeric props can be used in arithmetic operations, such as calculating a total from price and quantity.
Q8. What happens if a number is passed as a string?
Answer: It remains a string unless converted. This can affect arithmetic and comparisons, so values should be validated and converted when necessary.
Q9. Can props be modified inside a child component?
Answer: Props should be treated as read-only. The parent should provide updated values, or state should be used when the component needs to manage changing data.
Q10. Why are props useful for reusable components?
Answer: Props allow one component to display different data in different situations without duplicating its structure or logic.
19. Quick Revision Checklist
- Understand what props are and how parent and child components communicate.
- Know how to pass string values using quotation marks.
- Know how to pass number values using curly braces.
- Understand the difference between a numeric string and a number.
- Use JavaScript variables and expressions inside JSX curly braces.
- Access values through
props.name or destructured parameters.
- Combine string and number props in reusable components.
- Perform calculations using validated numeric values.
- Use default prop values when appropriate.
- Remember that props are read-only.
20. Conclusion
Passing strings and numbers is a fundamental React skill. String props allow components to receive text such as names, titles, and descriptions, while number props provide values such as prices, ages, quantities, and scores. JSX uses quotation marks for literal strings and curly braces for numeric values, JavaScript variables, and expressions.
By understanding these patterns, developers can build reusable components, display dynamic information, perform calculations, and keep React applications organized and maintainable. Practice with product cards, student profiles, and course cards to strengthen your understanding of props.
21. React JS Training Resources
Learn more through the following course and demo links: