Skip to content

Conversation

cfilipponi
Copy link
Contributor

When accessing a non-existing property, JavaScript returns undefined.
The check for 'undefined' will guarantee that browserLang has been and we can safely call .indexOf()

When accessing a non-existing property, JavaScript returns undefined.
The check for 'undefined' will guarantee that browserLang has been and we can safely call .indexOf()
@cfilipponi
Copy link
Contributor Author

Another approach would be wrapping everything in a conditional like this:

if (browserLang) {
      if (browserLang.indexOf('-') !== -1) {
        browserLang = browserLang.split('-')[0];
      }

      if (browserLang.indexOf('_') !== -1) {
        browserLang = browserLang.split('_')[0];
      }
    }

@ocombe ocombe merged commit 3e0aebb into ngx-translate:master Feb 5, 2020
@ocombe ocombe changed the title fix(TranslateService): check whether variable contains undefined value fix(service): check whether browserLang is undefined Feb 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants