How to scroll to the top of a webpage using JavaScript?

February 4, 2021 - 1 min read

To scroll to the top of a webpage using JavaScript, you can use the scrollTo() method in the global window object and pass 0 as the first and second argument to the method.

// Scroll to Top of webpage
window.scrollTo(0, 0);
  • The first argument is the X Coordinate or the value you want to scroll in the horizontal direction.
  • The second argument is the Y Coordinate or the value you want to scroll in the vertical direction.

In our case, we are setting the first and second arguments as 0 to scroll to the top and to scroll to the left of the webpage.

See this example live in JSBin.

Feel free to share if you found this useful 😃.