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

getPlatform

Returns a string representing the user's platform.

TypeScript
Copied!
1/**
2 * Returns a string representing the user's platform.
3 *
4 * Possible values: 'iOS', 'Android', 'Mac', 'Windows', 'Linux', 'Unknown'.
5 */
6export function getPlatform(): string {
7  const ua = typeof navigator !== 'undefined' ? navigator.userAgent : '';
8
9  if (/android/i.test(ua)) return 'Android';
10  if (/iPad|iPhone|iPod/.test(ua) && !window.MSStream) return 'iOS';
11  if (/Macintosh/.test(ua)) return 'Mac';
12  if (/Windows/.test(ua)) return 'Windows';
13  if (/Linux/.test(ua)) return 'Linux';
14
15  return 'Unknown';
16}
  • Straightforward Output

    Returns a clean, human-readable platform label like 'iOS' or 'Windows', making it easy to use directly in UIs or logs.

  • Lightweight Implementation

    Uses basic string pattern matching with no dependencies, ensuring fast execution and minimal overhead.

  • Cross-Platform Awareness

    Covers all major platforms including mobile (iOS, Android) and desktop (Windows, macOS, Linux).

  • Safe Environment Check

    Gracefully handles non-browser environments by defaulting to an empty string for userAgent.

Tests | Examples

TypeScript
Copied!
1function mockUserAgent(ua: string) {
2  Object.defineProperty(window.navigator, 'userAgent', {
3    value: ua,
4    configurable: true,
5  });
6}
7
8test('getPlatform - detects Android', () => {
9  mockUserAgent('Mozilla/5.0 (Linux; Android 10; SM-G975F)');
10  expect(getPlatform()).toBe('Android');
11});
12
13test('getPlatform - detects iOS', () => {
14  mockUserAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X)');
15  expect(getPlatform()).toBe('iOS');
16});
17
18test('getPlatform - detects Mac', () => {
19  mockUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)');
20  expect(getPlatform()).toBe('Mac');
21});
22
23test('getPlatform - detects Windows', () => {
24  mockUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64)');
25  expect(getPlatform()).toBe('Windows');
26});
27
28test('getPlatform - detects Linux', () => {
29  mockUserAgent('Mozilla/5.0 (X11; Linux x86_64)');
30  expect(getPlatform()).toBe('Linux');
31});
32
33test('getPlatform - falls back to Unknown', () => {
34  mockUserAgent('SomeUnknownAgent');
35  expect(getPlatform()).toBe('Unknown');
36});

Common Use Cases

  • Platform-Specific UI Adjustments

    Customize layout, features, or interactions depending on the user's platform.

  • Analytics and Logging

    Record platform data for telemetry, usage patterns, or bug reports.

  • Feature Flags or Conditional Logic

    Enable or disable code paths based on whether the user is on mobile or desktop.

  • Platform-Specific Downloads or Prompts

    Show OS-appropriate app download links or installation instructions.

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