this is the code
<div className="flex-box d-flex justify-content-between align-items-center">
<h6>Price</h6>
<span>
$ {product.price.toLocaleString('en-US', {maximumFractionDigits: 2,})}
</span>
</div>
it shows no error
but when I look at it, it shows this
how do I solve this? I just want to price to be like $ 1,399.99
2
Answers
use Intl.NumberFormat for number formating
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
According to the error message, on the run time, you don’t have the
product.price
so it results in "undefined".you have to check for its availability before using any other methods chained to it.
you can simply do this:
By adding ? you are using optional chaining. and you won’t see the error.