JavaScript Array.toLocaleString() Method | W3Schools
Examples
Format according to regional formats:
const prices = [1234.56, 7890.12, 3456.78];
const dateArray = [new Date(), new Date('2023-01-01')];
console.log(prices.toLocaleString('en-US'));
console.log(prices.toLocaleString('de-DE'));
console.log(dateArray.toLocaleString('en-US'));
console.log(dateArray.toLocaleString('ja-JP'));
Definition and Usage
The `Array.toLocaleString()` method is a built-in method of the JavaScript array object that returns a string representing the elements of the array.
The special feature of the `Array.toLocaleString()` method is that it formats each element in the array according to the current locale settings of the environment.
In simple terms, this method:
- Converts the array into a string
- Automatically handles localization formatting (such as dates, numbers, etc.)
- Separates elements with commas
Basic Syntax
array.toLocaleString([locales[, options]])
Parameter Description
- `locales` (optional): A string or an array of strings representing language codes (e.g., "en-US", "zh-CN")
- `options` (optional): A configuration object used to specify formatting options
How to Use
Basic Usage
const numbers =[12345.67,1000,3.14159];
console.log(numbers.toLocaleString('en-US'));
Output: "12,345.67,1,000,3.142"
console.log(numbers.toLocaleString('de-DE'));
Output: "12.345,67,1.000,3,142"
Handling Date Objects
const dates =[new Date(),new Date('2023-01-01')];
console.log(dates.toLocaleString('en-US'));
Possible output: "1/2/2024, 2:30:45 PM,1/1/2023, 12:00:00 AM"
Advanced Usage
Using the `options` Parameter
const numbers =[12345.67,1000,3.14159];
// Specify currency format
console.log(numbers.toLocaleString('en-US', {
style:'currency',
currency:'USD'
}));
Output: "$12,345.67,$1,000.00,$3.14"
// Specify percentage format
console.log([0.25,0.5,0.75].toLocaleString('en-US', {
style:'percent'
}));
Output: "25%,50%,75%"
Difference from `toString()`
| Method | Localization Handling | Date Formatting | Number Formatting |
|---|---|---|---|
| `toString()` | No | Converts to default string | No special handling |
| `toLocaleString()` | Yes | Formats according to local settings | Formats according to local settings |
const arr =[1000,new Date()];
console.log(arr.toString());
Output: "1000,Wed Jan 03 2024 14:30:00 GMT+0800 (China Standard Time)"
console.log(arr.toLocaleString('zh-CN'));
Output: "1,000,2024/1/3 14:30:00"
Practical Application Scenarios
- Multi-language Websites: Display formatted data based on user language preferences
- Financial Applications: Correctly display currency formats for different regions
- Data Analysis: Present numbers and dates in formats familiar to users
- Form Displays: Show localized data in input fields
Precautions
- Performance Considerations: Frequent calls may affect performance, especially with large arrays
- Browser Compatibility: Although modern browsers support it, some options may vary
- Default Behavior: If no `locales` parameter is specified, the runtime environment's default language settings will be used
- Element Processing: For non-date/non-number objects, its `toString()` method will be called
Summary
The `Array.toLocaleString()` method is a powerful localization tool, particularly suitable for scenarios where formatted data needs to be displayed according to the user's region. It simplifies internationalization (i18n) implementation, allowing developers to easily handle differences in number, date, and other formats across regions.
Remember, when your application needs to serve global users, using this method can greatly enhance the user experience!
YouTip