The toLocaleString() method returns a string representing the elements of the array. The elements are converted to strings using their toLocaleString methods and are separated by a locale-specific string.
The syntax of Array.toLocaleString() method is:
toLocaleString(): string
This toLocaleString() method of Array returns a string representation of an array. The elements are converted to string using their toLocaleString methods.
Array.toLocaleString() returns value of type string
.
The toLocaleString() method can be used to convert a numeric array to a locale-specific string representation.
For example,
arr
with numeric values [1000, 2000, 3000].toLocaleString()
to convert the array to a locale-specific string.result
.result
to the console.const arr = [1000, 2000, 3000];
const result = arr.toLocaleString();
console.log(result);
1,000,2,000,3,000
The toLocaleString() method can be used to convert an array of Date objects to a locale-specific string representation.
For example,
arr
with Date objects.toLocaleString()
to convert the array to a locale-specific string.result
.result
to the console.const arr = [new Date('2024-01-01'), new Date('2024-12-31')];
const result = arr.toLocaleString();
console.log(result);
1/1/2024, 12/31/2024
The toLocaleString() method can be used to convert an array with mixed types to a locale-specific string representation.
For example,
arr
with mixed values [12345.67, new Date('2024-01-01'), true].toLocaleString()
to convert the array to a locale-specific string.result
.result
to the console.const arr = [12345.67, new Date('2024-01-01'), true];
const result = arr.toLocaleString();
console.log(result);
12,345.67, 1/1/2024, true
In this TypeScript tutorial, we learned about toLocaleString() method of Array: the syntax and few working examples with output and detailed explanation for each example.