There are multiple ways to churn out a string version of a variable. Take Arrays for example, there are 3: join
, toString
, toLocaleString
.
But what’s the difference?
If our array is the plain old kind:
var arr = [1, 2, 3];
Then the 3 would all return a "1,2,3"
, since the default separator for join
is a comma, and the latter 2 do the same trick. Of course, with join, we can specify what the separator is. But what about difference between toString
and toLocaleString
?
Simple answer is: the Locale version is location specific. When it comes to Date
s.
The Array version of toLocaleString
doesn’t provide much space for manual configurations, but looking at Date
‘s parameters, we see that the results could be specified for different regions. The same goes with other data types that comes with both toString
and toLocaleString
:
Object: Object.prototype.toLocaleString() Number: Number.prototype.toLocaleString() Date: Date.prototype.toLocaleString()
Without Date
s meddling, it’s always safe to use just the plain old toString()
.