I've been looking through A re-introduction to javascript and came across the array method toLocaleString().
I did some googling (something to do with localization?) but am having difficulty figuring it out.
What is a locale? What's the difference between toString() and toLocaleString() in javascript?
I've been looking through A re-introduction to javascript and came across the array method toLocaleString().
I did some googling (something to do with localization?) but am having difficulty figuring it out.
What is a locale? What's the difference between toString() and toLocaleString() in javascript?
Share Improve this question asked Feb 26, 2015 at 7:43 Luke DieboldLuke Diebold 1532 silver badges10 bronze badges 1-
It's like how dates or numbers are written differently amongst languages, like American (mm/dd/yyyy) vs British (dd-mm-yyyy) which can be observed with
Date.prototype.toLocaleString()
. – Ja͢ck Commented Feb 26, 2015 at 7:45
1 Answer
Reset to default 4toString()
this will just convert the string format without any conversion at all (unless you try to overwrite it).
toLocaleString()
will convert to string format and apply a conversion base on Locale. Example: if your Locale is set to US (U.S. English locale)
var number = 3500
console.log(number.toLocaleString());
This will print (in the console): "3,500". But if you were using only toString()
you would get "3500" (without the ma for the thousands).
It works for currencies, dates, times, etc...
More information here: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString