What is the role of scrollY property in JavaScript?

HTMLJavascriptProgramming Scripts

The scrollY property in JavaScript works the same as pageYoffset property. If you want to get the pixels the document scrolled to from the upper left corner of the window, then use the scrollY property for vertical pixels.


You can try to run the following code to learn how to work with scrollY property in JavaScript.

<!DOCTYPE html>
         div {
            background-color: yellow;
            height: 1500px;
            width: 1500px;

         function scrollFunc() {
            window.scrollBy(200, 200);
            alert("Vertical: " + window.scrollY);
      <button onclick="scrollFunc()" style="position:fixed;">Scroll</button>
Published on 26-Feb-2018 15:10:09