Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Array.toLocaleString() method

JavaScript Array.toLocaleString() method

Array Object Reference JavaScript Array object


Example

Format according to regional formats:

const

prices

=

[

1234.56

,

7890.12

,

3456.78

]

;

const

dateArray

=

[

new

Date

(

)

,

new

Date

(

'

2023-01-01

'

)

]

;

//

Number arrays using localized format

console

.

log

(

prices

.

toLocaleString

(

'

en-US

'

)

)

;

//

Output similar to: "1,234.56,7,890.12,3,456.78" (English format)

console

.

log

(

prices

.

toLocaleString

(

'

de-DE

'

)

)

;

//

Output similar to: "1.234,56,7.890,12,3.456,78" (German format)

//

Date arrays using localized format

console

.

log

(

dateArray

.

toLocaleString

(

'

en-US

'

)

)

;

//

Output similar to: "5/6/2023, 1/1/2023" (English date format)

console

.

log

(

dateArray

.

toLocaleString

(

'

ja-JP

'

)

)

;

//

Output similar to: "2023/5/6, 2023/1/1" (Japanese date format)

Try it »


Definition and Usage

Array.toLocaleString() is a built-in method of the JavaScript Array object that returns a string representing the elements of the array.

Array.toLocaleString() The special feature of this method is that it formats each element of the array according to the language setting (locale) of the current environment.

Simply put, this method:


Basic Syntax


array.toLocaleString([locales[, options]])

Parameter Description


How to Use

Basic Usage

Example

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

Example

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

Use the options parameter

Example

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() None Convert to default string No special handling
toLocaleString() Yes According to local format According to local format

Example

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

  1. Multilingual websites: Display formatted data according to the user's language preference
  2. Financial applications: Correctly display currency formats for different regions
  3. Data analysis: Display numbers and dates in formats familiar to the user
  4. Form display: Display localized format data in input fields

Notes

  1. Performance considerations: Frequent calls may affect performance, especially on large arrays
  2. Browser compatibility: Although modern browsers all support it, some options may differ
  3. Default behavior: If the locales parameter is not specified, the default language setting of the runtime environment is used
  4. Element handling: For non-date/number objects, their toString() method is called

Summary

Array.toLocaleString() is a powerful localization tool, especially suitable for scenarios that need to display formatted data based on the user's region. It simplifies the implementation of internationalization (i18n), allowing developers to easily handle format differences in numbers, dates, etc. across regions.

Remember, when your application needs to serve global users, using this method can greatly improve the user experience!

Array Object Reference JavaScript Array object