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 sizewindow.location→ URL infowindow.navigator→ browser infowindow.screen→ device screen detailswindow.history→ navigation historywindow.document→ access DOM
Ques: What are some important window methods?
window.alert()– Displays an alert boxwindow.confirm()– Asks for confirmationwindow.prompt()– Takes user inputwindow.open()– Opens a new window or tabwindow.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 resolutionscreen.availWidth,screen.availHeight→ available space excluding OS barsscreen.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 URLlocation.hostname→ domain namelocation.pathname→ file pathlocation.protocol→ http / httpslocation.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 buttonhistory.forward()– Same as next buttonhistory.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 detailsnavigator.language→ user’s preferred languagenavigator.onLine→ check if onlinenavigator.platform→ OS infonavigator.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?
-
Alert box
alert("Welcome!"); -
Confirm box
if (confirm("Are you sure?")) console.log("Yes"); -
Prompt box
let name = prompt("Enter your name:"); alert("Hello " + name);
Ques: What is the difference between alert(), confirm(), and prompt()?
| Method | Description | Returns |
|---|---|---|
alert() | Displays a message | undefined |
confirm() | Asks user to confirm | true / false |
prompt() | Takes user input | text / null |
Ques: What are JavaScript timing events?
Ans: Timing events execute code after a delay or repeatedly.
setTimeout()→ executes once after delaysetInterval()→ repeats at intervalsclearTimeout()/clearInterval()→ stop timers
Example:
setTimeout(() => console.log("Hello after 2s"), 2000);
Ques: What is the difference between setTimeout() and setInterval()?
| Method | Description |
|---|---|
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?
| Feature | DOM | BOM |
|---|---|---|
| Full Form | Document Object Model | Browser Object Model |
| Purpose | Interacts with page content | Interacts with browser environment |
| Main Object | document | window |
| Example | document.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
