Learn how JavaScript talks directly to the browser — showing alerts, capturing user input, and controlling the window through the BOM.
The Browser Object Model (BOM) allows JavaScript to interact
with the browser window and its components — such as tabs, alerts, navigation,
and user dialogs — through objects like window.
Unlike the DOM (which deals with page content), the BOM deals with the browser itself: its size, its location bar, its popups, and the environment your script is running in.
BOM means: "JavaScript talking directly to the browser." It lets you:
console.log(typeof window);
console.log(typeof window.alert);
console.log(typeof window.location);
window is the global object in every browser tabalert, location, history — live on itconsole.log("Before alert");
// alert("Welcome to JavaScript"); // shows popup — commented for runner
console.log("After alert");
alert() shows a popup message — it blocks all code until the user clicks OK// Simulating confirm() — returns boolean
let result = true; // true = user clicked OK, false = Cancel
console.log(result);
console.log(typeof result);
confirm() returns true when the user clicks OK, false when they click Cancel// Simulating prompt() — always returns a string
let name = "Arjun"; // what the user would type
console.log(name);
console.log(typeof name);
prompt() always returns a string — even if the user types a numbernull// Simulating prompt() input
let age = "15"; // prompt always returns a string
if (age < 18) {
console.log("Underage");
} else {
console.log("Allowed");
}
prompt() returns a string, so age is "15" (string), not 15 (number). JavaScript coerces it here, but strict comparisons (===) or arithmetic will break.
let age = Number("15"); // convert before comparing
console.log(age);
console.log(typeof age);
if (age < 18) {
console.log("Underage");
} else {
console.log("Allowed");
}
Think of a website asking you questions or giving you information:
BOM dialogs work inside real HTML pages. Click ▶ Preview to run this live — interact with the buttons to trigger each dialog type.
<!DOCTYPE html>
<html>
<head><title>BOM Demo</title></head>
<body style="font-family:sans-serif;padding:24px;background:#f8f9fa">
<h2>BOM Dialog Demo</h2>
<p id="output" style="margin:12px 0;color:#333;min-height:24px"></p>
<button onclick="runAlert()"
style="margin:6px;padding:8px 18px;background:#f7df1e;border:none;
border-radius:6px;cursor:pointer;font-weight:700">
alert()
</button>
<button onclick="runConfirm()"
style="margin:6px;padding:8px 18px;background:#4ade80;border:none;
border-radius:6px;cursor:pointer;font-weight:700">
confirm()
</button>
<button onclick="runPrompt()"
style="margin:6px;padding:8px 18px;background:#82aaff;border:none;
border-radius:6px;cursor:pointer;font-weight:700">
prompt()
</button>
<script>
const out = document.getElementById("output");
function runAlert() {
alert("Hello from BOM!");
out.textContent = "alert() fired — you clicked OK.";
}
function runConfirm() {
let result = confirm("Do you want to continue?");
out.textContent = "confirm() returned: " + result;
}
function runPrompt() {
let name = prompt("Enter your name:");
if (name) {
out.textContent = "prompt() got: " + name;
} else {
out.textContent = "prompt() returned null (Cancel clicked).";
}
}
</script>
</body>
</html>
// Simulate prompt input
let userName = "Riya";
prompt() to ask the user for their namealert() — e.g. "Hello, Riya!"// Simulate prompt + confirm
let age = Number("20");
let confirmed = true;
prompt() to ask the user's age — convert with Number()confirm() to double-check before showing resultconfirm() return?
confirm() returns true when the user clicks OK and false when they click Cancel — always a boolean.
prompt() opens an input field dialog — alert() shows a message and confirm() asks a yes/no question, but neither accepts typed input.
window in the browser?
console.log(typeof window);
window is the top-level global object in the browser environment. Every variable you declare at the top level and every BOM function lives on it.
All BOM functions are properties of window — you can call them with or without the prefix:
alert("Hello"); // short form
window.alert("Hello"); // full form — identical result
console.log(typeof window.alert);
Always convert prompt() output with Number() when you expect a numeric value:
let raw = "42"; // what prompt() would return
let value = Number(raw);
console.log(value);
console.log(typeof value);
Avoid excessive alert() calls — they block the browser and frustrate users. Modern apps prefer HTML-based modals and toast notifications instead.
BOM dialogs are most useful for testing, small interactions, and debugging. For anything production-grade, build a proper UI component instead.
Read the code carefully — will "Equal" ever appear? Think before you run.
// Simulating prompt() input
let value = "10"; // prompt() always returns a string
if (value === 10) {
console.log("Equal");
} else {
console.log("Not Equal");
}
// Step 1: prompt() always returns a STRING
// value = "10" (type: string)
// Step 2: === checks value AND type
// "10" === 10 → false (string !== number)
// Step 3: "Not Equal" is always printed
// Fix: convert the input before comparing
let value = Number("10"); // value = 10 (type: number)
if (value === 10) {
console.log("Equal"); // ✓ now this works
}