isBrowser
Determines if the current runtime environment is a browser so that front-end modules can run on the server (Node) without throwing errors.
- Use
Array.prototype.includes()
on thetypeof
values of bothwindow
anddocument
(globals usually only available in a browser environment unless they were explicitly defined), which will returntrue
if one of them isundefined
. typeof
allows globals to be checked for existence without throwing aReferenceError
.- If both of them are not
undefined
, then the current environment is assumed to be a browser.