Javascript: How to extract language from <html lang="...">

If your have a HTML page with the lang attribute set:

<html lang="de">
<!-- ... -->
</html>

you can extract the language code using Javascript like this:

const lang = document.documentElement.lang;

In case you want to specify a default language, use this variant:

const lang = document.documentElement.lang || 'en';

This will default to 'en' if the lang attribute is not set.