📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
The localStorage object is a type of web storage that allows Javascript websites and apps to store and access data right in the browser with no expiration date. This means the data stored in the browser will persist even after the browser window has been closed. The localStorage property object is read-only.
In this guide, we will learn below all 5 localStorage object methods with example.
JavaScript LocalStorage APIs/Methods
- setItem(): Add key and value to LocalStorage
- getItem(): Retrieve a value by the key from LocalStorage
- removeItem(): Remove an item by key from LocalStorage
- clear(): Clear all LocalStorage
- key(): Passed a number to retrieve nth key of a LocalStorage
Syntax
window.localStorage
localStorage.setItem("key", "value");
var lastname = localStorage.getItem("key");
localStorage.removeItem("key");
localStorage.clear();
var KeyName = localStorage.key(index);
localStorage.setItem() API/Method
localStorage.setItem("firstName", "Ramesh");
var user = {
firstName : "Ramesh",
lastName : "Fadatare"
}
localStorage.setItem("id", JSON.stringify(user));
localStorage.getItem() API/Method
localStorage.getItem('id');
"{"firstName":"Ramesh","lastName":"Fadatare"}"
JSON.parse(localStorage.getItem('id'));
localStorage.removeItem() API/Method
localStorage.removeItem('id');
localStorage.clear() API/Method
localStorage.clear();
localStorage.key() API/Method
var KeyName = localStorage.key(index);
Comments
Post a Comment
Leave Comment