Skip to content
Related Articles

Related Articles

Improve Article

JavaScript | Intl.ListFormat.prototype.formatToParts() Method

  • Last Updated : 16 Apr, 2020

The Intl.ListFormat.prototype.formatToParts() method is an inbuilt method in JavaScript which returns an Array of objects representing the different components that can be used to format a list of values in a locale-aware fashion.

Syntax:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

Intl.ListFormat.prototype.formatToParts(list)

Parameters: This method accepts single parameter as mentioned above and described below:

  • list: This parameter holds an Array of values to be formatted according to a locale.

Return Value: This method returns an Array of components that contains the formatted parts from the list.



Below examples illustrate the Intl.ListFormat.prototype.formatToParts() method in JavaScript:

Example 1:




<script>
const gfg = ['Geeks1', 'Geeks2', 'Geeks3'];
const result = new Intl.ListFormat('en-GB',
    { style: 'long', type: 'conjunction' });
  
let val = result.formatToParts(gfg);
  
console.log(val[0]);
console.log(val[1]);
console.log(val[2]);
console.log(val[3]);
console.log(val[4]);
</script>

Output:

Object { type: "element", value: "Geeks1" }
Object { type: "literal", value: ", " }
Object { type: "element", value: "Geeks2" }
Object { type: "literal", value: " and " }
Object { type: "element", value: "Geeks3" }

Example 2:




<script>
const gfg = ['Geeks1', 'Geeks2', 'Geeks3'];
const result = new Intl.ListFormat('hi',
    { style: 'long', type: 'conjunction' });
  
let val = result.formatToParts(gfg);
  
console.log(val[0]);
console.log(val[1]);
console.log(val[2]);
console.log(val[3]);
console.log(val[4]);
</script>

Output:

Supported Browsers: The browsers supported by Intl.ListFormat.prototype.formatToParts() method are listed below:

  • Google Chrome
  • Opera



My Personal Notes arrow_drop_up
Recommended Articles
Page :