The Object toLocaleString() method
By Flavio Copes
Learn how the JavaScript toLocaleString() method returns a string representation of an object and accepts an optional locale argument to customize it.
The toLocaleString() method returns a string representation of an object, meant to be adapted to a locale, meaning a language and region.
Called on a plain object instance, it returns the [object Object] string, unless overridden:
const person = { name: 'Fred' }
person.toLocaleString() //[object Object]
That’s because the version defined on Object.prototype does nothing locale-related. It just calls toString(). So why does it exist at all?
Why does this method exist?
It’s a hook. JavaScript defines it on Object.prototype so that every object has it, and the types where locale formatting makes sense override it with a useful implementation.
Numbers, dates, and arrays all do. That’s where this method gets interesting:
const price = 1234567.89
price.toLocaleString('en-US') //1,234,567.89
price.toLocaleString('it-IT') //1.234.567,89
Same number, different separators. English uses commas for thousands and a dot for decimals. Italian does the opposite.
Dates get the same treatment:
const date = new Date(2026, 7, 7)
date.toLocaleString('it-IT') //07/08/2026, 00:00:00
An array calls toLocaleString() on each of its elements and joins the results with commas. Handy when a list mixes numbers and dates and you want them all localized in one call.
Number’s version also accepts an options object as second argument, for things like currency formatting:
const price = 42
price.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' })
//42,00 €
Objects can override it too
Since it’s just a method, your own objects can return a different string representation depending on the locale:
const temperature = {
celsius: 20,
toLocaleString(locale) {
if (locale === 'en-US') {
return `${this.celsius * 1.8 + 32}°F`
}
return `${this.celsius}°C`
}
}
temperature.toLocaleString('en-US') //68°F
temperature.toLocaleString('it-IT') //20°C
Code that formats values for display can now call toLocaleString() on anything, without knowing the type, and get something sensible back.
One pitfall
On a plain object, the locale argument is silently ignored:
const person = { name: 'Fred' }
person.toLocaleString('it-IT') //[object Object]
No error, no warning, just the useless default. If you’re passing a locale and getting [object Object] back, you’re calling the base implementation. Either format the individual number and date properties instead, or override the method as shown above.
Want me to talk about your product? You can sponsor this site.
Related posts about js: