Clean β’ Professional
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 DOMQues: 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 windowExample:
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 displayQues: 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 pageQues: 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 timersExample:
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?
Ques: What are alternatives to cookies in modern web apps?
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?