Array.prototype.includes()
on the typeof
values of both window
and document
(globals usually only available in a browser environment unless they were explicitly defined), which will return true
if one of them is undefined
.typeof
allows globals to be checked for existence without throwing a ReferenceError
.undefined
, then the current environment is assumed to be a browser.const isBrowser = () => ![typeof window, typeof document].includes('undefined');
isBrowser(); // true (browser)
isBrowser(); // false (Node)
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️