# Scroll to top of the page

Scroll the window back to the top.

Canonical: https://1loc.dev/dom/scroll-to-top-of-the-page/
Version: 1.0.0
Runtime: JavaScript · modern browsers (DOM)
Verification: Usage lines executed in headless Chromium against the fixture markup; no external services
Updated: 2026-10-28

## Implementation

```javascript
function goToTop() { window.scrollTo(0, 0); }
```

## Usage

Markup:

```html
<div style="height: 3000px"></div>
```

Script:

```javascript
window.scrollTo(0, 1500);
goToTop();
window.scrollY; // → 0
```

Lines marked → were executed in headless Chromium against the markup above, and each result was compared with the value shown. Lines marked e.g. are illustrative.

## Limits

- If the page sets scroll-behavior: smooth, the scroll animates. Keep smooth scrolling inside a prefers-reduced-motion: no-preference query.
- When the jump is a navigation step, also move focus, for example to the main heading, so keyboard and screen-reader users follow.

The function does not validate its arguments. Add checks where it receives untrusted input.

## Source and license

Original: https://github.com/phuocng/1loc/blob/14b481cf60dff1620ab1bb99a43b1f68068cdd9b/contents/scroll-to-top-of-the-page.mdx
Author: Nguyen Huu Phuoc and original contributors
Contributors: See original repository history
Source commit: 14b481cf60dff1620ab1bb99a43b1f68068cdd9b
Copyright (c) 2020 phuoc-ng
License: MIT — https://1loc.dev/licenses/
Maintainer: Oveyon
Modifications: Documented as a reference with executed usage lines. Original MDX retained as inert source evidence.
