JavaScript .toLocaleString()
JavaScript array’s .toLocaleString() method converts the elements of an array to localized string 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.
Syntax
array.toLocaleString(locales, options)
Parameters:
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.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.
Return value:
Returns a string representing the elements of the array, with each element converted using its toLocaleString() method and joined by a locale-specific separator.
Example 1: Basic .toLocaleString() Usage
This example demonstrates how .toLocaleString() converts array elements to their locale-specific string representations:
// Create an array with different data typesconst mixedArray = [1000, new Date('2024-01-15'), 3.14159];// Convert to locale string using default localeconst result = mixedArray.toLocaleString();console.log(result);
The output will vary based on the user’s locale, but in a US English locale, it would produce something like:
1,000,1/15/2024, 12:00:00 AM,3.142
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.
Example 2: Currency Formatting with .toLocaleString()
This example shows how to format an array of prices using locale-specific currency formatting:
// Array of product pricesconst prices = [29.99, 149.5, 5.25, 999.99];// Format as US currencyconst usPrices = prices.map((price) =>price.toLocaleString('en-US', {style: 'currency',currency: 'USD',})).join(', ');console.log('US Prices:', usPrices);// Format as European currencyconst euroPrices = prices.toLocaleString('de-DE', {style: 'currency',currency: 'EUR',});console.log('Euro Prices:', euroPrices);
This example results in the following output:
US Prices: $29.99, $149.50, $5.25, $999.99Euro Prices: 29,99 €, 149,50 €, 5,25 €, 999,99 €
The method automatically applies the appropriate currency symbols, decimal separators, and formatting conventions for each specified locale.
Codebyte Example: Date Internationalization with .toLocaleString()
This example demonstrates formatting an array of dates for different regions and showing various date formatting options:
The dates are formatted according to each locale’s conventions, showing how the same data can be presented in culturally appropriate ways.
Frequently Asked Questions
1. What is the basic purpose of the .toLocaleString()?
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.
2. What is the difference between toString and toLocaleString in JavaScript?
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.
3. Can .toLocaleString() handle sparse arrays with undefined elements?
Yes, .toLocaleString() treats sparse arrays (arrays with gaps) by converting undefined and null elements to empty strings, but still includes separators for them.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours