DEV Community

Cover image for How to scroll to the top of a webpage using JavaScript?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

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

Originally posted here!

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);
Enter fullscreen mode Exit fullscreen mode
  • 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 😃.


Top comments (0)