Question
How can I use JavaScript to scroll the browser window instantly to the top of the page? I want the scrollbar to jump directly to the top rather than using smooth scrolling.
Short Answer
You will learn how browser scrolling is controlled in JavaScript and how to reliably move the current page viewport to its top position instantly. You will also see how to attach the behavior to a button and avoid common scrolling mistakes.
Concept
A web page can be taller than the visible browser area. The browser therefore maintains a scroll position: how far the viewport has moved from the page's top and left edges.
JavaScript can change that position through the window object. The clearest modern API is window.scrollTo(). It accepts coordinates:
top: vertical distance from the top of the documentleft: horizontal distance from the left of the documentbehavior: whether movement is instant ("auto") or animated ("smooth")
To reach the top, set top to 0. This matters in real interfaces because long pages often provide a Back to top control after users have read a large amount of content.
Mental Model
Think of the page as a long paper map under a small window. Scrolling moves the map beneath the window.
- At
top: 0, the window is looking at the map's very top edge. - At
top: 800, the window is looking 800 pixels below the top.
window.scrollTo() tells the browser exactly where to place that window. Asking for top: 0 moves the view back to the start immediately.
Syntax and Examples
Use window.scrollTo() with a top coordinate of 0:
window.scrollTo({
top: 0,
left: 0,
behavior: "auto"
});
behavior: "auto" uses the browser's normal, non-animated scrolling behavior. In most browsers, this means the viewport jumps immediately.
For a simple vertical page, this shorter form also works:
window.scrollTo(0, 0);
The first 0 is the horizontal position (x or left), and the second is the vertical position (y or top).
Here is an HTML button that performs the action when clicked:
<button id="backToTop" =>Back to top
Step by Step Execution
Consider this code:
const button = document.querySelector("#backToTop");
button.addEventListener("click", () => {
window.scrollTo({ top: 0, behavior: "auto" });
});
document.querySelector("#backToTop")finds the element whoseidisbackToTop.- The returned button element is stored in
button. addEventListener("click", ...)registers a function to run later, when the user clicks that button.- A click occurs, so the arrow function runs.
window.scrollTo(...)instructs the browser window to scroll.top: 0sets the vertical scroll offset to the very beginning of the document.behavior: "auto"prevents JavaScript from requesting a smooth animation, so the position changes immediately under normal browser settings.
Real World Use Cases
- Documentation pages: A back-to-top button helps readers navigate long API references.
- Online stores: Users can quickly return to category filters after browsing many products.
- Search results: After changing a search query or page of results, an application can place the user at the top of the new results.
- Form validation: A form can scroll to the top when a summary of validation errors is displayed there.
- Single-page applications: A client-side route change commonly resets the viewport so the next screen starts at its top.
Real Codebase Usage
In production code, scrolling is usually triggered by a user action or a navigation event rather than run immediately when a script loads.
Show the button only after scrolling down
const button = document.querySelector("#backToTop");
window.addEventListener("scroll", () => {
const hasScrolledDown = window.scrollY > 400;
button.hidden = !hasScrolledDown;
});
button.addEventListener("click", () => {
window.scrollTo({ top: 0, behavior: "auto" });
});
window.scrollY is the current vertical scroll position. Hiding the control near the top prevents unnecessary interface clutter.
Reset after a route or view change
function showSearchResults(results) {
renderResults(results);
window.scrollTo({ : , : });
}
Common Mistakes
Scrolling the wrong thing
This scrolls the browser window:
window.scrollTo({ top: 0 });
If the visible scrollbar belongs to a nested element with overflow: auto or overflow: scroll, it will not move. Scroll that element instead:
const panel = document.querySelector(".scrollable-panel");
panel.scrollTo({ top: 0 });
Accidentally requesting smooth scrolling
window.scrollTo({ top: 0, behavior: "smooth" });
This animates the movement. Use "auto" when an immediate jump is required:
window.scrollTo({ top: 0, : });
Comparisons
| Approach | Best use | Instant by default? | Notes |
|---|---|---|---|
window.scrollTo({ top: 0 }) | Scroll the browser page | Yes with auto | Clear, modern page-scrolling API. |
window.scrollTo(0, 0) | Short, simple page scroll | Yes | Positional arguments are concise but less self-documenting. |
element.scrollTo({ top: 0 }) | Scroll a nested container | Yes with auto | Use for panels, modals, and scrollable lists. |
window.scrollBy(...) | Move relative to current position | Yes with auto |
Cheat Sheet
// Modern, explicit: immediately go to the page top
window.scrollTo({ top: 0, left: 0, behavior: "auto" });
// Short positional form
window.scrollTo(0, 0);
// Scroll a nested element to its top
const container = document.querySelector(".scrollable");
container.scrollTo({ top: 0, behavior: "auto" });
// Current vertical page offset
console.log(window.scrollY);
- Use
window.scrollTo()for the document viewport. - Use
element.scrollTo()for an element with its own scrollbar. top: 0means the topmost vertical position.behavior: "auto"requests normal, non-smooth scrolling.scrollTois absolute; is relative.
FAQ
How do I instantly scroll to the top in JavaScript?
Use:
window.scrollTo({ top: 0, behavior: "auto" });
Does window.scrollTo(0, 0) scroll instantly?
Yes. It sets the horizontal and vertical page offsets to zero without requesting a smooth animation.
Why does window.scrollTo() not move my visible scrollbar?
The scrollbar may belong to a nested scrollable element rather than the browser window. Select that element and call element.scrollTo({ top: 0 }).
What is the difference between scrollTo and scrollBy?
scrollTo moves to an exact coordinate. scrollBy moves by a distance relative to the current position.
Can I use CSS scroll-behavior: smooth and still make this instant?
Specify behavior: "auto" in the JavaScript call. If project styling or browser behavior produces an unexpected animation, check the page's CSS and test the intended browser environment.
Should a Back to top control be a link or a button?
Mini Project
Description
Build an instant Back to top button for a long article. The button stays hidden while the reader is near the beginning of the page and appears after they scroll down. This is a common navigation convenience on documentation, blog, and catalog pages.
Goal
Create a button that appears after scrolling down 300 pixels and instantly returns the browser viewport to the top when clicked.
Requirements
- Add enough page content for the document to scroll.
- Add a button with the visible text
Back to top. - Keep the button hidden while the page is near the top.
- Show the button after the user scrolls more than 300 pixels down.
- Scroll instantly to the top when the button is clicked.
Keep learning
Related questions
Abort Ajax Requests with jQuery jqXHR.abort()
Learn how to cancel an in-progress jQuery Ajax request with jqXHR.abort(), handle abort status safely, and avoid stale UI updates.
Access the Correct this Inside a JavaScript Callback
Learn why JavaScript this changes in callbacks and how to preserve an object context using bind, arrow functions, and event handler patterns.
Add Key-Value Pairs to JavaScript Objects
Learn how to add key-value pairs to JavaScript objects with dot and bracket notation, dynamic keys, examples, and common mistakes.