Let's see how we can pass numeric values as prop
.
In the below example, the Fahrenheit
component receives a numeric prop celsius
and displays the corresponding Fahrenheit value.
In the above code snippet, we have passed the celsius
value inside curly braces celsius={100}
.
We can also pass celsius
value as a string like this:
This is possible because of type coercion in JavaScript. Type coercion is the automatic conversion of values from one data type to another.
However, passing a numeric value as a string is error-prone due to the inconsistencies in how values are converted between different data types in JavaScript.
For example: "100" * 2 would result in 200, but "100" + 100 would result in "100100". The multiplication operator converts a string to a number, and the addition operator converts a number to a string if one of the arguments is a string and the other is a number.