Skip to content

Commit fc69025

Browse files
[Term Entry] JavaScript Arrays: toLocaleString() (#7350)
1 parent ae04f6f commit fc69025

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
Title: '.toLocaleString()'
3+
Description: 'Converts array elements to localized string representations and joins them with locale-specific separators.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Web Development'
7+
Tags:
8+
- 'Arrays'
9+
- 'Methods'
10+
- 'Strings'
11+
CatalogContent:
12+
- 'introduction-to-javascript'
13+
- 'paths/front-end-engineer-career-path'
14+
---
15+
16+
JavaScript array's **`.toLocaleString()`** method converts the elements of an [array](https://www.codecademy.com/resources/docs/javascript/arrays) to localized [string](https://www.codecademy.com/resources/docs/javascript/strings) representations and joins them with a locale-specific separator. This method is particularly useful when working with arrays containing numbers, dates, or other values that need to be formatted according to specific regional conventions, making data more readable and culturally appropriate for different audiences.
17+
18+
## Syntax
19+
20+
```pseudo
21+
array.toLocaleString(locales, options)
22+
```
23+
24+
**Parameters:**
25+
26+
- `locales` (optional): A string with a BCP 47 language tag, or an array of such strings. Specifies the locale(s) to use for formatting. If not provided, the default locale of the host environment is used.
27+
- `options` (optional): An object with configuration properties that control how the conversion is performed. The available options depend on the type of elements being converted.
28+
29+
**Return value:**
30+
31+
Returns a string representing the elements of the array, with each element converted using its `toLocaleString()` method and joined by a locale-specific separator.
32+
33+
## Example 1: Basic `.toLocaleString()` Usage
34+
35+
This example demonstrates how `.toLocaleString()` converts array elements to their locale-specific string representations:
36+
37+
```js
38+
// Create an array with different data types
39+
const mixedArray = [1000, new Date('2024-01-15'), 3.14159];
40+
41+
// Convert to locale string using default locale
42+
const result = mixedArray.toLocaleString();
43+
console.log(result);
44+
```
45+
46+
The output will vary based on the user's locale, but in a US English locale, it would produce something like:
47+
48+
```shell
49+
1,000,1/15/2024, 12:00:00 AM,3.142
50+
```
51+
52+
Each element is formatted according to the default locale rules, with numbers getting thousand separators, dates formatted in MM/DD/YYYY format, and decimal numbers rounded appropriately.
53+
54+
## Example 2: Currency Formatting with `.toLocaleString()`
55+
56+
This example shows how to format an array of prices using locale-specific currency formatting:
57+
58+
```js
59+
// Array of product prices
60+
const prices = [29.99, 149.5, 5.25, 999.99];
61+
62+
// Format as US currency
63+
const usPrices = prices
64+
.map((price) =>
65+
price.toLocaleString('en-US', {
66+
style: 'currency',
67+
currency: 'USD',
68+
})
69+
)
70+
.join(', ');
71+
72+
console.log('US Prices:', usPrices);
73+
74+
// Format as European currency
75+
const euroPrices = prices.toLocaleString('de-DE', {
76+
style: 'currency',
77+
currency: 'EUR',
78+
});
79+
console.log('Euro Prices:', euroPrices);
80+
```
81+
82+
This example results in the following output:
83+
84+
```shell
85+
US Prices: $29.99, $149.50, $5.25, $999.99
86+
Euro Prices: 29,99 €, 149,50 €, 5,25 €, 999,99 €
87+
```
88+
89+
The method automatically applies the appropriate currency symbols, decimal separators, and formatting conventions for each specified locale.
90+
91+
## Codebyte Example: Date Internationalization with `.toLocaleString()`
92+
93+
This example demonstrates formatting an array of dates for different regions and showing various date formatting options:
94+
95+
```codebyte/javascript
96+
// Array of important dates
97+
const importantDates = [
98+
new Date('2024-07-04'),
99+
new Date('2024-12-25'),
100+
new Date('2024-01-01')
101+
];
102+
103+
// Format for US audience with full date names
104+
const usFormat = importantDates.map(date =>
105+
date.toLocaleString('en-US', {
106+
weekday: 'long',
107+
year: 'numeric',
108+
month: 'long',
109+
day: 'numeric',
110+
})
111+
).join(', ');
112+
113+
console.log('US Format:', usFormat);
114+
115+
// Format for Japanese audience
116+
const japanFormat = importantDates.toLocaleString('ja-JP', {
117+
year: 'numeric',
118+
month: 'short',
119+
day: 'numeric'
120+
});
121+
console.log('Japan Format:', japanFormat);
122+
```
123+
124+
The dates are formatted according to each locale's conventions, showing how the same data can be presented in culturally appropriate ways.
125+
126+
## Frequently Asked Questions
127+
128+
### 1. What is the basic purpose of the `.toLocaleString()`?
129+
130+
The `.toLocaleString()` method formats array elements according to locale-specific conventions, making data more readable and culturally appropriate for users in different regions. It handles numbers, dates, and other values by applying regional formatting rules like currency symbols, date formats, and number separators.
131+
132+
### 2. What is the difference between toString and toLocaleString in JavaScript?
133+
134+
The `toString()` method converts array elements to strings using a standard format regardless of locale, while `toLocaleString()` applies locale-specific formatting rules. For example, `toString()` would display `1000` as "1000", but `toLocaleString()` might display it as "1,000" in English locales or "1.000" in German locales.
135+
136+
### 3. Can `.toLocaleString()` handle sparse arrays with undefined elements?
137+
138+
Yes, `.toLocaleString()` treats sparse arrays (arrays with gaps) by converting `undefined` and `null` elements to empty strings, but still includes separators for them.

0 commit comments

Comments
 (0)