For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/predicate/isBrowser.md.

isBrowser es-toolkit

Checks if the current execution environment is a browser.

const result = isBrowser();

Usage

isBrowser()

Use isBrowser when you have code that should only run in browser environments. It determines if the environment is a browser by checking for the existence of window.document. Useful in SSR (Server-Side Rendering) or Node.js environments.

import { isBrowser } from 'es-toolkit/predicate';

// Manipulate DOM only in browser environment
if (isBrowser()) {
  document.getElementById('app').innerHTML = 'Hello World';
  console.log('Running in browser environment');
} else {
  console.log('Running in server environment');
}

Can be used to implement conditional logic based on the environment.

import { isBrowser } from 'es-toolkit/predicate';

function getWindowWidth() {
  if (isBrowser()) {
    return window.innerWidth;
  }
  return 0; // Return default value on server
}

// Registering event listeners
function addWindowListener() {
  if (isBrowser()) {
    window.addEventListener('resize', () => {
      console.log('Window size changed');
    });
  }
}

Particularly useful in SSR frameworks like Next.js or Nuxt.js.

import { isBrowser } from 'es-toolkit/predicate';

function initializeAnalytics() {
  if (isBrowser()) {
    // Load analytics script only in browser
    const script = document.createElement('script');
    script.src = 'https://analytics.example.com/script.js';
    document.head.appendChild(script);
  }
}

// Accessing local storage
function getStoredValue(key: string) {
  if (isBrowser()) {
    return localStorage.getItem(key);
  }
  return null;
}

Returns

Returns true if the current environment is a browser, false otherwise.

boolean
Source: es-toolkit

Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.