JavaScript BOM Interview Questions and Answers

Understand browser-provided window, location, history, storage, and navigator APIs.

Frequently Asked Questions

Important questions are highlighted in the sections below; select one to jump to its answer.

Theoretical JavaScript Interview Questions

window.alert() pops up a browser-native dialog with your message and an OK button. The catch? It completely freezes your script and the page until the user dismisses it. That's why it's mostly used for quick debugging during development or critical warnings. Overusing alerts trains users to click OK without reading, which defeats the purpose. For anything user-facing in production, use toast notifications or modals instead.

window.alert() wins on simplicity: zero setup, works everywhere, guaranteed to grab attention. But that's about where the advantages end.

The downsides are real: it blocks all JavaScript execution, you can't style it, you can't add custom buttons, and it looks different on every browser. Custom modals and toast notifications take more effort to build, but they integrate with your design, support rich content, don't freeze the page, and let users keep interacting. For anything beyond a quick dev-time debug message, custom UI wins every time. The tradeoff is development time vs. user experience.

The BOM is everything the browser exposes beyond the DOM - basically the browser itself as an API. window is the global object (all globals are technically properties of window). location lets you read or redirect the current URL. history gives you back/forward navigation and pushState for SPA routing. navigator is where you check things like userAgent, language, or geolocation. screen is rarely used in practice, but tells you about the display resolution.

The BOM (Browser Object Model) is a set of objects provided by the browser that let JavaScript interact with the browser environment beyond the page content. The root object is window, which also gives access to history, navigator, screen, location, and Web Storage APIs. Unlike the DOM, the BOM has no official standard but is consistently supported across all modern browsers.

Both store key-value pairs in the browser with the same API (setItem, getItem, removeItem, clear). The difference is lifetime: localStorage data persists indefinitely until explicitly cleared, surviving tab and browser restarts. sessionStorage data is cleared automatically when the browser tab is closed. Both are scoped to the current origin (protocol + domain + port).

Cookies are sent to the server automatically with every HTTP request and support an expiry date; they are limited to about 4 KB. localStorage is client-side only (never sent to the server), has a larger capacity of around 5 MB, and has no built-in expiry. Use cookies for server-side authentication and session data; use localStorage for client-only preferences and caching.

window.location is an object that contains the current page URL broken into parts: href (full URL), hostname, pathname, search (query string), and hash. You can read these to inspect the URL or set location.href to navigate the browser to a new page. location.reload() reloads the current page.

window.navigator is a BOM object that contains information about the user's browser and device. Common properties include userAgent (browser and OS identification string), language (user's preferred language), onLine (whether the browser has network access), and cookieEnabled. It is often used for browser detection and feature checks.

`pushState()` adds a same-document session history entry with supplied state and URL, without loading that URL. The URL must satisfy the same-origin restrictions for the document.

`replaceState()` updates the current session history entry instead of adding a new one. It does not itself trigger a `popstate` event.

`popstate` is fired when the active history entry changes, commonly after Back or Forward navigation to an entry created or modified by the History API. Calling `pushState()` or `replaceState()` alone does not fire it.

Use small serializable state needed to restore the view associated with a history entry. Browsers may persist state, so avoid large payloads and values that cannot be serialized.

It must update the rendered view itself because `pushState()` changes the URL and history but does not load or render the destination. It should also handle `popstate` to restore the view when users navigate Back or Forward.

Browsers can serialize and persist history state, impose implementation-specific size limits, and may throw if state cannot be cloned. Store compact restoration data and keep large application data elsewhere.

Read `event.state` and the current URL, then render the corresponding view without pushing another entry. The app should also tolerate `null` state for entries it did not create.

No. A supplied URL must be same-origin with the current document or the call throws a `SecurityError`; it may also be relative or omitted. The call changes the displayed URL but does not navigate to or verify the resource.

The Browser Object Model is the collection of browser objects for interacting with the browser environment, including window, location, history, and navigator. The DOM instead represents document contents.

In a browser page, window represents the browsing context and is the global object for scripts in that context. It exposes APIs such as document, location, and viewport information.

Location exposes URL components such as href, origin, pathname, and search. Assigning a new URL to href typically initiates navigation.

The History API can add or replace same-origin history entries with pushState() and replaceState(). It does not grant access to arbitrary prior pages or their contents.

Browser security boundaries prevent a page from inspecting cross-origin documents in browsing history. pushState and replaceState alter entries for the current origin but do not bypass the same-origin policy.

Storage can be quota-limited, cleared by users, evicted under pressure, or unavailable in restricted contexts. Applications should handle failures and avoid treating it as the sole copy of irreplaceable data.

Feature detection checks whether the needed capability exists instead of guessing from a browser brand or version. It is more resilient to new browsers, changed user agents, and partial implementations.

pushState adds a same-origin URL and structured-cloneable state without immediately loading the URL. It does not fire popstate when called, and cross-origin URLs are prohibited.

JavaScript Practical and Coding Interview Questions

Problem: Create a small wrapper around an object with getItem and setItem methods. Return null for a missing key, parse saved JSON when reading, and serialize values when writing. Use an in-memory adapter so the example runs without browser permissions.

javascript

function createJsonStorage(storage) {
  return {
    read(key) {
      const raw = storage.getItem(key);
      return raw === null ? null : JSON.parse(raw);
    },
    write(key, value) {
      storage.setItem(key, JSON.stringify(value));
    },
  };
}

const values = new Map();
const memoryStorage = {
  getItem(key) { return values.has(key) ? values.get(key) : null; },
  setItem(key, value) { values.set(key, value); },
};
const store = createJsonStorage(memoryStorage);
store.write('trail', { distance: 12, unit: 'km' });
console.log(store.read('trail'));
console.log(store.read('missing'));