J

JavaScript Handbook

Clean • Professional

JavaScript Browser BOM — Top Interview Questions & Answers

4 minute

JavaScript Browser BOM — Interview Questions & Answers

Ques: What is the Browser Object Model (BOM)?

Ans: The BOM (Browser Object Model) allows JavaScript to interact with the browser environment — not just the webpage (DOM).

It gives access to browser-specific objects like window, navigator, screen, location, history, and more.

Example:

console.log(window.innerWidth); // Get browser width

Ques: What is the window object in JavaScript?

Ans: The window object is the top-level global object in browsers.

All global JavaScript variables, functions, and objects automatically become properties of window.

Example:

window.alert("Hello!");
console.log(window.innerHeight);

Ques: What are some commonly used window properties?

  • window.innerHeight, window.innerWidth → browser’s viewport size
  • window.location → URL info
  • window.navigator → browser info
  • window.screen → device screen details
  • window.history → navigation history
  • window.document → access DOM

Ques: What are some important window methods?

  • window.alert() – Displays an alert box
  • window.confirm() – Asks for confirmation
  • window.prompt() – Takes user input
  • window.open() – Opens a new window or tab
  • window.close() – Closes the current window

Example:

if (confirm("Are you sure?")) {
  alert("Confirmed!");
}

Ques: What is the screen object in JavaScript?

Ans: The screen object provides information about the user’s display monitor.

Example:

console.log(screen.width);
console.log(screen.height);

Common properties:

  • screen.width, screen.height → screen resolution
  • screen.availWidth, screen.availHeight → available space excluding OS bars
  • screen.colorDepth → bit depth for color display

Ques: What is the location object used for?

Ans: The location object represents the current URL and allows redirection or reloading of pages.

Example:

console.log(location.href); // Full URL
location.href = "<https://google.com>"; // Redirects

Common properties & methods:

  • location.href → full URL
  • location.hostname → domain name
  • location.pathname → file path
  • location.protocol → http / https
  • location.reload() → reloads page

Ques: How can you reload or redirect a webpage using JavaScript?

location.reload(); // Reloads current page
location.href = "<https://example.com>"; // Redirects to another page

Ques: What is the history object in JavaScript?

Ans: The history object allows navigation through the browser’s session history.

Example:

history.back();  // Go to previous page
history.forward(); // Go to next page

Methods:

  • history.back() – Same as browser back button
  • history.forward() – Same as next button
  • history.go(n) – Moves n steps in history (negative for back)

Ques: What is the navigator object?

Ans: The navigator object gives information about the browser and system environment.

Example:

console.log(navigator.userAgent);
console.log(navigator.language);

Common properties:

  • navigator.userAgent → browser details
  • navigator.language → user’s preferred language
  • navigator.onLine → check if online
  • navigator.platform → OS info
  • navigator.geolocation → access location (with permission)

Ques: How do you detect the browser type in JavaScript?

console.log(navigator.userAgent);

Example Output:

Mozilla/5.0 (Windows NT 10.0; Win64; x64)

Ques: How can you display popups in JavaScript?

  1. Alert box

    alert("Welcome!");
    
  2. Confirm box

    if (confirm("Are you sure?")) console.log("Yes");
    
  3. Prompt box

    let name = prompt("Enter your name:");
    alert("Hello " + name);
    

Ques: What is the difference between alert(), confirm(), and prompt()?

MethodDescriptionReturns
alert()Displays a messageundefined
confirm()Asks user to confirmtrue / false
prompt()Takes user inputtext / null

Ques: What are JavaScript timing events?

Ans: Timing events execute code after a delay or repeatedly.

  • setTimeout() → executes once after delay
  • setInterval() → repeats at intervals
  • clearTimeout() / clearInterval() → stop timers

Example:

setTimeout(() => console.log("Hello after 2s"), 2000);

Ques: What is the difference between setTimeout() and setInterval()?

MethodDescription
setTimeout(fn, delay)Runs code once after delay
setInterval(fn, delay)Runs code repeatedly every interval

Example:

setInterval(() => console.log("Repeating..."), 1000);

Ques: How can you stop a timer in JavaScript?

let id = setInterval(() => console.log("Running"), 1000);
clearInterval(id); // stops repeating

Ques: What are cookies in JavaScript?

Ans: Cookies are small pieces of data stored on the user’s browser to remember information (like login or preferences).

Example:

document.cookie = "username=John; expires=Fri, 31 Dec 2025 12:00:00 UTC; path=/";

Ques: How can you read cookies using JavaScript?

console.log(document.cookie);

Ques: How can you delete a cookie?

Ans: Set the cookie’s expiry date to the past:

document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC;";

Ques: What are the limitations of cookies?

  • Limited to ~4KB per cookie
  • Stored as plain text (less secure)
  • Sent with every request → increases load
  • Can be disabled by users

Ques: What are alternatives to cookies in modern web apps?

  • LocalStorage – Persistent storage (no expiry)
  • SessionStorage – Temporary per-session data
  • IndexedDB – Structured large storage

Ques: What is the difference between BOM and DOM?

FeatureDOMBOM
Full FormDocument Object ModelBrowser Object Model
PurposeInteracts with page contentInteracts with browser environment
Main Objectdocumentwindow
Exampledocument.getElementById()window.alert()

Ques; How can you detect if the user is online or offline?

if (navigator.onLine) {
  console.log("Online");
} else {
  console.log("Offline");
}

Ques: How can you get the user’s location using BOM?

navigator.geolocation.getCurrentPosition(
  (pos) => console.log(pos.coords.latitude, pos.coords.longitude),
  (err) => console.error(err)
);

Ques: What is the window.open() method used for?

Ans: Opens a new browser tab or window:

window.open("<https://example.com>", "_blank");

Ques: What are common use cases of the BOM?

  • Detecting browser or screen details
  • Redirecting users
  • Managing cookies or sessions
  • Handling browser navigation
  • Displaying popups or alerts
  • Setting timeouts and intervals

Article 0 of 0