Detect the Browser Language Preference with JavaScript

10/05/2021

Contents

In this article, you will learn how to detect the browser language preference with JavaScript.

Navigator.language

To get the most preferred language of the user, you can use Navigator.language property.
This read-only property returns a string representing the most preferred language set in the web browser.

const lang = navigator.language;
console.log(lang);  // "en-US"

Navigator.languages

To get the user’s preferred languages, you can use Navigator.languages property.
This read-only property returns an array of DOMStrings representing the user’s preferred languages set in the web browser.
The returned languages are ordered by preference with the most preferred language first.

const langList = navigator.languages;
console.log(langList);  // ["en-US", "ja"]