Friday, September 19, 2025
HomeLanguagesJavascriptHow to detect the browser language preference using JavaScript ?

How to detect the browser language preference using JavaScript ?

Detecting the language preference of users can be very important for Websites or Web Apps to increase user interaction. By using JavaScript, this task can be easily done by using:

Languages property is available for the navigator interface, which returns the most preferred / user-preferred language set in the web browser. This property is read-only.

Syntax:

navigator.languages 

// Or

navigator.language

Return Value: 

  • The navigator.languages property will return an array that stores the languages in an order in which the language most preferred by the user will be the first element.
  • The navigator.language property will return the first element of the array which is returned by the navigator.languages property i.e. the most preferred user language.

Note: Language property is a read-only property, thus it is only possible for us to get the value, we cannot make changes to the user preferred language.

Example 1: Getting the most preferred language.

Javascript




<script>
    var usrlang = navigator.language 
            || navigator.userLanguage;
    console.log(
        "User's preferred language is: "
        + usrlang);
</script>


Output:

User's preferred language is: en-US

Example 2: Getting the preferred language array.

Javascript




<script>
    var usrlang = navigator.languages;
    console.log(usrlang);
</script>


Output:

['en-US', 'en']
0:"en-US"
1:"en"
length
:2
RELATED ARTICLES

Most Popular

Dominic
32301 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6666 POSTS0 COMMENTS
Nicole Veronica
11840 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11898 POSTS0 COMMENTS
Shaida Kate Naidoo
6781 POSTS0 COMMENTS
Ted Musemwa
7056 POSTS0 COMMENTS
Thapelo Manthata
6739 POSTS0 COMMENTS
Umr Jansen
6744 POSTS0 COMMENTS