How to Save & Restore Scroll Position in JavaScript (localStorage Tutorial)

Published: 22 June 2025
on channel: LearnAwesome
618
6

In this JavaScript tutorial, you’ll learn how to save and restore the scroll position of a webpage using localStorage. This is super useful for long pages like blogs, dashboards, or any site where users should return to the same spot after a page reload.

⚠ Correction: At 01:58 in the video, the if (scrollY !== null) check is incorrect. It should be if (savedY !== null) — otherwise it always runs and may cause issues when savedY is null. Note scrollY is always a positive number as it is a property of window object, so code seems to work correctly even though its working by chance.
You’ll learn how to:

Track scroll position using window.scrollY

Save it to localStorage with setItem

Restore it on window.load using scrollTo()

💡 Technologies used:

JavaScript DOM Events

scrollY and scrollTo()

localStorage API

#JavaScript #localStorage #ScrollPosition #WebDev #Frontend


On this page of the site you can watch the video online How to Save & Restore Scroll Position in JavaScript (localStorage Tutorial) with a duration of hours minute second in good quality, which was uploaded by the user LearnAwesome 22 June 2025, share the link with friends and acquaintances, this video has already been watched 618 times on youtube and it was liked by 6 viewers. Enjoy your viewing!