Question
What is the difference between a browser API and a library?
Answer
An API exposes capabilities built into the browser itself, while a library is external code you add to make existing capabilities more convenient.
| Aspect | API | Library |
|---|---|---|
| Source | Built into browser | External code you include |
| Capabilities | Access features beyond pure JS | Convenience, not new features |
| Examples | Canvas, Geolocation, WebStorage, WebGL, WebSockets | jQuery, Bootstrap, Vue.js, W3.CSS, React |
Key distinction:
- APIs extend what JavaScript can do (access hardware, storage, graphics)
- Libraries make existing capabilities easier to use (could implement yourself)
Example: The Geolocation API lets JavaScript access GPS hardware - impossible with pure JS. Bootstrap just provides pre-styled CSS classes - you could write the CSS yourself.
Note saved — thanks!
Question
What is the Geolocation API used for?
Answer
The Geolocation API lets a web app ask "where is this device right now?" and get back coordinates — latitude and longitude — straight from the browser.
Plain JavaScript has no way to find out where you physically are; the page only knows what you typed. The Geolocation API bridges that gap by letting the browser tap into whatever positioning hardware the device has — GPS on a phone, or Wi-Fi and IP-based estimates on a laptop. That single capability — "the device knows it's at 47.05° N, 8.31° E" — is what powers a huge range of features:
- Route planning and navigation — a maps app needs your start point to draw a route.
- Booking services (Uber, AirBnB) — "find a ride/room near me" only works if "me" has a location.
- Weather forecasts — show the forecast for where you are without making you type a city.
- Sports tracking — record the path of a run by sampling position over time.
- Local search — "restaurants nearby", "closest ATM".
A key thing to understand is the round trip: the browser only gives you raw coordinates. To turn 47.05, 8.31 into something useful ("the three closest pizzerias"), your code typically sends those coordinates to a server which does the lookup and sends results back. The browser locates; the server interprets.
Because location is sensitive personal data, the browser always asks the user for permission first with a prompt, and the user can deny it. Your code must handle that "no" gracefully rather than assuming a position will always arrive. (This is also why geolocation only works over HTTPS — covered in its own card.)
Memory tip: the API answers where, the server answers so what.
Go deeper:
MDN — Geolocation API — the canonical overview: methods, the permission model, and the secure-context requirement.
Note saved — thanks!