Home > Article > Web Front-end > What does localstorage store?
LocalStorage can store multiple types of data, including strings, numbers, Boolean values, objects, arrays, etc. LocalStorage is a web storage mechanism that allows data to be stored in a user's browser that persists across the user's multiple browser sessions. Compared with Cookies, LocalStorage has larger storage space and longer validity period, so it is more suitable for storing large amounts of data or long-term data.
The operating system for this tutorial: Windows 10 system, DELL G3 computer.
LocalStorage can store multiple types of data, including strings, numbers, Boolean values, objects, arrays, etc. This is because the storage mechanism of LocalStorage is based on the JavaScript String type, but other types of data can be stored through some techniques and methods.
LocalStorage is a web storage mechanism that allows data to be stored in the user's browser that can persist across the user's multiple browser sessions. Compared with Cookies, LocalStorage has larger storage space and longer validity period, so it is more suitable for storing large amounts of data or long-term data.
In LocalStorage, you can use the setItem() method to store data in key-value pairs, use the getItem() method to obtain stored data, use the removeItem() method to delete data, and use the clear() method to clear all stored data. These methods are implemented based on the JavaScript Storage interface.
It should be noted that the data in LocalStorage is stored in the form of key-value pairs. Each key-value pair has a unique key name and corresponding value. Key names and values are both string types, but other types of data can be stored through some tricks and methods. For example, you can use the JSON.stringify() method to convert an object or array into a string for storage, and then use the JSON.parse() method to convert the string back into an object or array for reading.
In addition, the data in LocalStorage is persistent and will not be lost even if the browser is closed or the computer is restarted. However, it should be noted that since LocalStorage is a browser-based storage mechanism, it may be affected by browser restrictions and security policies. For example, some browsers may limit LocalStorage storage space or encrypt it.
Cookies and LocalStorage differ in the following aspects:
1. Storage size: The maximum storage capacity of a single cookie is 4k. If it is larger than 4k, the storage fails and the corresponding file cannot be found in the browser. Cookie information; the maximum storage capacity of LocalStorage is 5m. If it is greater than this maximum limit, the browser prompts an error.
2. Validity period: Cookie is session-level storage by default (coexisting with the tab page), and the expiration time can be set; LocalStorage is persistent storage unless it is cleared actively.
3. Operability: Cookie not only stores data, but also has many other attributes for its operation and setting. For example, Domain and Path together determine the scope of the cookie. If Secure is set to true, then Cookies can only be sent to the server using the https protocol; LocalStorage only stores data.
4. Portability: Cookie will be carried when requesting, but LocalStorage will not. Cookie information from the same origin will be automatically sent to the server as part of the request header. If too many cookies are set, it will increase the communication load. LocalStorage does not have this problem, it will always exist on the browser side.
In short, LocalStorage is a very useful web storage mechanism that can help us store various types of data in the user's browser, and these data can be stored between the user's multiple browser sessions Persistent presence. However, it should be noted that since LocalStorage is a browser-based storage mechanism, it may be affected by browser restrictions and security policies. Therefore, its suitability and security need to be carefully considered when using LocalStorage.
The above is the detailed content of What does localstorage store?. For more information, please follow other related articles on the PHP Chinese website!