DEV Community

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

Posted on • Originally published at melvingeorge.me

How to scroll to the bottom of the webpage using JavaScript?

Originally posted here!

To scroll to the bottom of the webpage using JavaScript, first, we need to get the full scrollable height of the window. The scrollable height is the total pixels away from the top of the webpage.

We can get that using the scrollHeight property in the document.body global object like this,

// get the scroll height of the window
const scrollHeight = document.body.scrollHeight;
Enter fullscreen mode Exit fullscreen mode

Now, all we need to do is to use the scrollTo() method in the global window object and pass 0 as the first argument and the scrollHeight as the second argument to it like this,

// get the scroll height of the window
const scrollHeight = document.body.scrollHeight;

// scroll to the bottom of webpage
window.scrollTo(0, scrollHeight);
Enter fullscreen mode Exit fullscreen mode

That's it 🌟!

See this example live in JSBin.

Feel free to share if you found this useful 😃.


Top comments (0)