Skip to content

isBrowser

Checks whether the code is currently running in a browser-like environment (window and window.document both defined).

Note: some hybrid test environments (e.g. happy-dom, jsdom) define both window and Node’s process at the same time — this only reports on window’s presence, it does not imply isNode is false. Use both together if you need to distinguish a real browser from a DOM-emulating Node test environment.

Available since v3.0.3

import { isBrowser } from '@helpers4/guard';
// or, from the all-in-one package (same code, one install):
import { isBrowser } from 'helpers4/guard';
isBrowser(): boolean

boolean — true if window and window.document are both defined

Useful for isomorphic code that behaves differently in the browser vs. Node.js.

if (isBrowser()) {
  localStorage.setItem('key', 'value')
} else {
  // Node.js / server-side fallback
}

View source on GitHub