Yevhen Klymentiev
dark
light
console
darkness
y.klymentiev@gmail.com
Reusable Snippets|Practical utility code for everyday use — custom-built and ready to share

isEdge

Determines if the current browser is Microsoft Edge.

TypeScript
Copied!
1/**
2 * Determines if the current browser is Microsoft Edge.
3 *
4 * @returns True if running in Edge, false otherwise.
5 */
6export function isEdge(): boolean {
7  if (typeof navigator === 'undefined') return false;
8  const ua = navigator.userAgent;
9  return /sEdg//.test(ua);
10}
  • Lightweight Implementation

    Uses a simple and fast regular expression that avoids overcomplicating browser detection logic.

  • Accurate Detection of Chromium-Based Edge

    Targets the "Edg/" token present in modern Edge user agents, effectively distinguishing it from legacy Internet Explorer.

  • Safe Runtime Usage

    Includes a typeof navigator guard to prevent errors in non-browser environments.

Tests | Examples

TypeScript
Copied!
1const originalNavigator = global.navigator;
2
3afterEach(() => {
4  global.navigator = originalNavigator;
5});
6
7test('returns true for Edge userAgent', () => {
8  // @ts-ignore
9  global.navigator = {
10    userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' +
11      '(KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.59'
12  };
13  expect(isEdge()).toBe(true);
14});
15
16test('returns false for Chrome userAgent', () => {
17  // @ts-ignore
18  global.navigator = {
19    userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' +
20      '(KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
21  };
22  expect(isEdge()).toBe(false);
23});
24
25test('returns false if navigator is undefined', () => {
26  // @ts-ignore
27  delete global.navigator;
28  expect(isEdge()).toBe(false);
29});

Common Use Cases

  • Edge-Specific Bug Fixes or Workarounds

    Apply layout or behavior adjustments tailored to quirks in the Edge rendering engine.

  • Conditional Feature Support

    Enable or disable features based on Edge’s unique API support, such as clipboard or file system APIs.

  • Analytics and Browser Reporting

    Log or segment users by browser type to monitor adoption trends or usage issues.

  • Custom UI Tweaks

    Adapt CSS or UI components when Edge behaves differently from Chrome or Firefox.

Codebase: Utilities -> Platforms -> isEdge | Yevhen Klymentiev