Modern web applications often need to store data on the user's browser so that information can be reused later. JavaScript provides two important storage mechanisms called Local Storage and Session Storage. These features allow developers to store data directly in the browser without using a database.
Both Local Storage and Session Storage are part of the Web Storage API. They store data as key–value pairs and allow websites to save information such as user preferences, login states, and application settings.
What is Web Storage?
Web Storage is a browser feature that allows JavaScript to store data locally in the user's browser. The stored data can be accessed later without needing a server request.
Local Storage
Local Storage allows developers to store data permanently in the browser. The stored data remains available even if the user refreshes the page, closes the browser, or restarts the system.
Local Storage is commonly used for saving user preferences, theme settings, and application data.
Storing Data in Local Storage
The value "Rahul" is stored in Local Storage with the key "username".
Retrieving Data from Local Storage
Rahul
Removing Data from Local Storage
The stored value with key "username" is removed from Local Storage.
Session Storage
Session Storage is similar to Local Storage but with one key difference: the stored data is available only during the browser session. Once the browser tab is closed, the stored data is automatically removed.
Session Storage is commonly used for temporary data such as session information or form data during a user's visit.
Storing Data in Session Storage
The value "Student" is stored in Session Storage.
Retrieving Data from Session Storage
Student
Difference Between Local Storage and Session Storage
- Local Storage stores data permanently in the browser.
- Session Storage stores data only for the current browser session.
- Local Storage data remains after closing the browser.
- Session Storage data is removed when the browser tab is closed.
Conclusion
Local Storage and Session Storage are powerful browser features that allow JavaScript to store data locally. They help developers build faster and more interactive web applications by reducing server requests and preserving user data.
Local Storage is suitable for long-term data storage, while Session Storage is ideal for temporary session-based information.
In the next tutorial, you will learn about Cookies in JavaScript, which is another method for storing small pieces of data in the browser.

