![]() |
The data stored using localStorage.setItem persists even after the browser is closed and reopened, making it useful for storing user preferences, session data, or other information that needs to be retained between sessions. Syntax:localStorage.setItem("key", string) In this method, we take two arguments as a key, which is a string representing the name of the item we want to store, and a value, which can be of any data type supported by the JSON format (e.g., strings, numbers, arrays, objects). Example 1: In this example, the we store string “Aayush” with the key “name” in the browser’s localStorage, then retrieve and print it.
Output: Example 2 : To demonstrate storing a number 30 with the key “age” in the browser’s localStorage, then retrieves, parses, and prints it.
Output: Example 3: In this example we store an array [“red”, “green”, “blue”] with the key “colors” in the browser’s localStorage after converting it to a JSON string, then retrieves, parses, and prints it.
Output: Example 4: In this example we store an object { name: ‘Atul’, age: 25, city: ‘India’ } with the key “person” in the browser’s localStorage after converting it to a JSON string, then retrieves, parses, and prints it.
Output: |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 18 |