# Get and set scroll position of the document using JavaScript

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Aug 12, 2020, updated Sep 23, 2021. Topics: JavaScript.
Canonical URL: https://attacomsian.com/blog/javascript-dom-get-and-set-scroll-position-of-the-document

> Learn how to get and set the scroll position of the document in JavaScript.

To get the scroll position of the document, you can use the `pageXOffset`, `pageYOffset` properties of the `window` object or the `scrollLeft` and `scrollTop` properties of the `documentElement` object.

Here is an example:

```javascript
const x = window.pageXOffset || document.documentElement.scrollLeft;
const y = scrollTop = window.pageYOffset || document.documentElement.scrollTop;
```

To reset the scroll position of the document, just update the `documentElement` and `body`'s `scrollLeft` and `scrollTop` properties:

```javascript
document.documentElement.scrollTop = document.body.scrollTop = 250;
document.documentElement.scrollLeft = document.body.scrollLeft = 300;
```
