Same point:
All three can be used to store data on the browser side, and they are all key-value pairs of string type.
the difference
characteristic | Cookie | LocalStorage | sessionStorage |
data life cycle | Generally generated by the server, the expiration time can be set. If generated in the browser, the default is to expire after closing the browser | Persistent unless cleared | Only valid for the current session, cleared after closing the page or browser |
storage data size | 4KB | Normally 5MB | Normally 5MB |
Communicate with the server side | It will be carried in the HTTP header every time. If you use cookies to save too much data, it will cause performance problems. | It is only stored in the client and does not participate in the communication with the server. (There are also scripts that choose to submit to the server side) | 同LocalStorage |
use | Generally generated by the server to identify the user identity | Used to restore pure data on the browser side | 同LocalStorage |
shared |
I hope my experience of entering the pit is helpful to you, may the Holy Light be with you