Open In App

JavaScript Intl DisplayNames() Constructor

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.

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/




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.




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:

We have a complete list of JavaScript Intl methods to check those please go through, the JavaScript Intl Reference article.


Article Tags :