JavaScript Interview Question #171

What is the difference between window.location and the History API?

Performance, Browser APIs, Rendering & Modern Web Architecture Senior Advanced

Quick Interview Answer

window.location represents the current document URL and can be used to navigate, reload, or inspect URL components. Assigning a new location normally causes browser navigation and document loading.

Detailed Explanation

window.location represents the current document URL and can be used to navigate, reload, or inspect URL components. Assigning a new location normally causes browser navigation and document loading.

The History API can change the displayed same-origin URL and session history without automatically loading a new document. Single-page applications use it together with JavaScript rendering and popstate handling.

Code Example

// Full navigation:
window.location.href = "/account";

// Client-side history update:
history.pushState(
    { view: "account" },
    "",
    "/account"
);