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 = ['Beginner1', 'Beginner2', 'Beginner3', 'Beginner4'];
      
    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: 

"Beginner1, Beginner2, Beginner3, and Beginner4"
"Beginner1, Beginner2, Beginner3, or Beginner4"
"Beginner1 Beginner2 Beginner3 Beginner4"

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 = ['Beginner1', 'Beginner2', 'Beginner3', 'Beginner4'];
      
    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: 

"Beginner1, Beginner2, Beginner3, और Beginner4"
"Beginner1, Beginner2, Beginner3 या Beginner4"
"Beginner1, Beginner2, Beginner3 Beginner4"

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