JavaScript Intl ListFormat format() Method
The Intl.ListFormat.prototype.format() method is an inbuilt method in JavaScript that returns a string with a language-specific representation of the list.
Syntax:
listFormat.format([list]);
Parameters: This method accepts a single parameter as mentioned above and described below:
- list: This parameter holds an iterable object, such as an Array.
Return Value: This method returns a language-specific formatted string representing the elements of the list.
The below examples illustrate the Intl.ListFormat.prototype.format() method in JavaScript:
Example 1: In this example, we will return a string from an array using the Intl.ListFormat.prototype.format() method in JavaScript.
javascript
<script>
const gfg = [ 'Geeks1' , 'Geeks2' , 'Geeks3' , 'Geeks4' ];
const result1 = new Intl.ListFormat( 'en' ,
{ style: 'long' , type: 'conjunction' });
console.log(result1.format(gfg));
const result2 = new Intl.ListFormat( 'db' ,
{ style: 'short' , type: 'disjunction' });
console.log(result2.format(gfg));
const result3 = new Intl.ListFormat( 'en' ,
{ style: 'narrow' , type: 'unit' });
console.log(result3.format(gfg));
</script>
|
Output:
"Geeks1, Geeks2, Geeks3, and Geeks4"
"Geeks1, Geeks2, Geeks3, or Geeks4"
"Geeks1 Geeks2 Geeks3 Geeks4"
Example 2: In this example, we will return a string from an array using the Intl.ListFormat.prototype.format() method in JavaScript.
javascript
<script>
const gfg = [ 'Geeks1' , 'Geeks2' , 'Geeks3' , 'Geeks4' ];
const result1 = new Intl.ListFormat( 'hi' ,
{ style: 'long' , type: 'conjunction' });
console.log(result1.format(gfg));
const result2 = new Intl.ListFormat( 'hi' ,
{ style: 'short' , type: 'disjunction' });
console.log(result2.format(gfg));
const result3 = new Intl.ListFormat( 'hi' ,
{ style: 'narrow' , type: 'unit' });
console.log(result3.format(gfg));
</script>
|
Output:
"Geeks1, Geeks2, Geeks3, और Geeks4"
"Geeks1, Geeks2, Geeks3 या Geeks4"
"Geeks1, Geeks2, Geeks3 Geeks4"
We have a complete list of Javascript Intl methods, to check those please go through the Javascript Intl Complete Reference article.
Supported Browsers: The browsers supported by Intl.ListFormat.prototype.format() method are listed below:
- Google Chrome 72 and above
- Edge 79 and above
- Firefox 78 and above
- Opera 60 and above
- Safari 14.1 and above
Last Updated :
24 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...