JavaScript Intl.DisplayNames Constructor is used for creating Intl.DisplayNames object. This constructor is created using the new keyword. If we create the constructor without the new keyword it will give a TypeError
Syntax:
new Intl.DisplayNames(loc, opt)
Parameters: It has two parameters both are optional.
- loc: It is a String or an array of Strings which allow the following value
- nu: It specifies the numbering system to be used which can be arab, bali, latn, tibt etc.
- opt: It is an object which can have properties like style, type, languageDisplay, fallback etc.
Returns: A DisplayNames object.
Below examples illustrate the JavaScript Intl DisplayNames() Constructor:
Example 1: In this example, we use the properties like dateTimeField and calendar/
Javascript
const example1 = new Intl.DisplayNames( "en" , { type: "dateTimeField" }); const example2 = new Intl.DisplayNames( "fr" , { type: "dateTimeField" }); const example3 = new Intl.DisplayNames( "en" , {type: "calendar" }); console.log(example1.of( "year" )); console.log(example2.of( "year" )); console.log(example3.of( "chinese" )); |
Output:
year année Chinese Calendar
Example 2: In this example, we will use properties like Language and LanguageDisplay with the constructor.
Javascript
const example1 = new Intl.DisplayNames( "en" , { type: "language" , languageDisplay: "dialect" , }); const example2 = new Intl.DisplayNames( "fr" , { type: "language" , languageDisplay: "standard" , }); console.log(example1.of( "en-GB" )); console.log(example2.of( "en-GB" )); |
Output:
British English anglais (Royaume-Uni)
Supported Browsers:
- Chrome
- Edge
- Firefox
- Opera
- Safari
We have a complete list of JavaScript Intl methods to check those please go through, the JavaScript Intl Reference article.