Geolocation: If the user denies the position

Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026

The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.


Remember the permission popup window the browser shows when we call one of the methods to get the position?

If the user denies that, we can intercept this scenario by adding an error handling function, as the second parameter to the methods getCurrentPosition() and watchPosition().

navigator.geolocation.getCurrentPosition(position => {
  console.log(position)
}, error => {
	console.error(error)
})

The object passed to the second parameter contains a code property to distinguish between error types:

  • 1 means permission denied
  • 2 means position unavailable
  • 3 means timeout

Lessons in this unit:

0: Introduction
1: Getting the user's position
2: Watching the position for changes
3: ▶︎ If the user denies the position
4: Adding more options