How to scroll to the top of the page with JavaScript?

Spread the love

Sometimes, we want to scroll to the top of the page with JavaScript.

In this article, we’ll look at how to scroll to the top of the page with JavaScript.

How to scroll to the top of the page with JavaScript?

To scroll to the top of the page with JavaScript, we use the window.scroll method.

For instance, we write

window.scroll({
  top: 0,
  left: 0,
  behavior: "smooth",
});

to call window.scroll to scroll the page toi the top.

We set top to 0 to scroll to the top.

behavior is set to 'smooth' lets us scroll smoothly.

Conclusion

To scroll to the top of the page with JavaScript, we use the window.scroll method.

Leave a Reply

Your email address will not be published. Required fields are marked *