JavaScript
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Types
-
Section 3: Strings and Numbers in Depth
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Objects and Arrays
-
Section 7: Maps, Sets, and Symbols
-
Section 8: Asynchronous JavaScript
-
Section 9: Object-Oriented and Prototypes
-
Section 10: The DOM
-
Section 11: Browser APIs
-
Section 12: Modern JavaScript (ES2015-ES2025)
-
Section 13: Functional Programming Patterns
-
Section 14: Error Handling and Debugging
-
Section 15: Testing
-
Section 16: Accessibility for JavaScript Developers
-
Section 17: Internationalization and Localization
-
Section 18: Performance
-
Section 19: Node.js Fundamentals
-
Section 20: Regular Expressions
-
Section 21: Design Patterns in JavaScript
-
Section 22: Security Basics
-
Section 23: Data Structures and Algorithms in JavaScript
-
Section 24: Practical Projects
-
Section 25: More Advanced Async Patterns
-
Section 26: More Object and Class Practice
-
Section 27: Working with Dates and Internationalization
-
Section 28: Web Components
-
Section 29: More DOM and Browser Practice
-
Section 30: Build Tooling for Vanilla JavaScript
-
Section 31: More Practice Projects
-
Section 32: Interview and Algorithm Practice
-
Section 33: Error Objects (MDN Reference)
-
Section 34: TypedArrays and Binary Data
-
Section 35: Reflection and Metaprogramming (MDN Reference)
-
Section 36: More Global Functions (MDN Reference)
94: The Notification API
I see this all the time when developers first dive into the Notification API: they assume it works like a standard alert() or a console.log(). They write a single line of code—new Notification("Time is up!")—refresh the page, and... nothing happens. They assume the browser is bugged or their code is broken.
The "Instant Trigger" Myth vs. The Permission Gatekeeper
The misconception is that the Notification API is a direct command to the OS. In reality, it's a request that must be vetted by the user first. Because notifications can be incredibly annoying (we've all dealt with those aggressive "Allow notifications?" popups on every random blog), browsers treat this as a high-privilege action. If you haven't explicitly requested and been granted permission, the browser will simply ignore your new Notification() call without throwing a loud error.
Here is how you actually handle the handshake. You have to check the current permission state and, if it's "default" (meaning the user hasn't decided yet), ask them for access:
async function notifyUser() {
// Check if the browser even supports notifications
if (!("Notification" in window)) {
console.log("This browser does not support desktop notifications.");
return;
}
// If permission is already granted, just fire it off
if (Notification.permission === "granted") {
showAlert();
}
// If we haven't asked yet, we need to request it
else if (Notification.permission !== "denied") {
const permission = await Notification.requestPermission();
if (permission === "granted") {
showAlert();
}
}
}
function showAlert() {
new Notification("Focus Session Complete!", {
body: "Your 25 minutes are up. Time to stretch!",
icon: "/assets/timer-icon.png"
});
}
notifyUser();
Beyond the Basic Text: Controlling the Payload
Once you've cleared the permission hurdle, you'll realize that a plain string as the first argument is pretty boring. I rarely use the basic constructor because it looks like a system error. To make a notification feel like a part of a professional app, you need the options object.
The body property allows you to provide a detailed description, and the icon property is essential for branding. But the real power comes from the event listeners. You can actually make the notification interactive by listening for a click event on the notification instance itself.
Imagine you're building a messaging app. You don't just want to tell the user "You have a message"; you want them to be brought directly to that conversation when they click the alert:
const notification = new Notification("New Message from Sarah", {
body: "Are we still meeting at 5pm?",
icon: "/images/sarah-avatar.jpg",
tag: "message-alert" // Using a tag prevents duplicate notifications from stacking
});
notification.onclick = (event) => {
event.preventDefault(); // Prevent the browser from focusing the window immediately
window.focus();
window.location.href = "/chats/sarah";
notification.close();
};
One quick tip: Notice that tag property. If you send ten notifications with the same tag, the browser will usually replace the previous one instead of flooding the user's screen with ten separate boxes. It's a small detail, but it's the difference between a helpful tool and "malware-adjacent" behavior.
📋 Practical Task
Build a Pomodoro Timer Notification System
Create a small application that simulates a Pomodoro timer. The app should have a "Start Timer" button that initiates a 5-second countdown (for testing purposes, rather than a full 25 minutes).
Your implementation must include the following:
- A permission check that requests notification access only when the "Start Timer" button is clicked.
- A
setTimeoutorsetIntervalthat triggers a notification exactly when the timer hits zero. - The notification must include a
bodytext telling the user to take a break and atagto ensure multiple timers don't spam the screen. - A
clickevent listener on the notification that logs "User acknowledged the break!" to the console and closes the notification.
There are no comments for now.