Update isBrowser.md

This commit is contained in:
Angelos Chalaris
2018-03-19 19:28:07 +02:00
committed by GitHub
parent 380794d749
commit 5736b0329f

View File

@ -1,11 +1,10 @@
### isBrowser ### isBrowser
Determines if the current runtime environment is a browser so that front-end modules can run on the server (Node) Determines if the current runtime environment is a browser so that front-end modules can run on the server (Node) without throwing errors.
without throwing errors.
Use `Array.includes()` on the `typeof` values of both `window` and `document` (globals usually only available in a Use `Array.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`.
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`.
`typeof` allows globals to be checked for existence without throwing a ReferenceError. If both of them are not `undefined`, then the current environment is assumed to be a browser. If both of them are not `undefined`, then the current environment is assumed to be a browser.
```js ```js
const isBrowser = () => ![typeof window, typeof document].includes('undefined'); const isBrowser = () => ![typeof window, typeof document].includes('undefined');