JavaScript-primer

Browser-Storage

Browser storage refers to the various mechanisms that are available for storing data in a web browser, such as cookies, local storage, and session storage. These mechanisms allow you to store data on the client-side, so that it can be accessed and modified by your web application, even after the page has been refreshed or the browser has been closed and reopened.

Cookies are small pieces of data that are stored on the client-side and sent back to the server with every request. They are typically used to store session-specific data, such as authentication tokens or user preferences. Cookies are limited in size and are stored in plain text, which makes them less suitable for storing large amounts of data or sensitive information.

Local storage and session storage are similar to cookies, but offer more storage space and additional features, such as the ability to store complex data types and to access data across multiple windows and tabs. Local storage stores data permanently, even after the browser has been closed and reopened, while session storage stores data for a single session, and is deleted when the browser is closed.

Here is an example of using local storage to store and retrieve data:

localStorage.setItem('userName', 'John');
const userName = localStorage.getItem('userName');
console.log(userName); // Output: "John"

In this example, the setItem() method is used to store a value in local storage, and the getItem() method is used to retrieve the value. The data stored in local storage is available even after the page has been refreshed or the browser has been closed and reopened.

Browser storage can be a useful tool for storing data on the client-side and for maintaining state across multiple sessions. It can help you to build more responsive and interactive web applications, and to create a more seamless user experience.

Session Storage

Session storage is a client-side storage mechanism that is similar to local storage, but stores data for a single session only. In other words, data stored in session storage is deleted when the user closes the browser or ends the browsing session.

Session storage is useful for storing data that is specific to a single session, such as authentication tokens or temporary data that is not needed after the session has ended. It can be accessed and modified using the sessionStorage object, which provides methods for storing and retrieving data, such as setItem(), getItem(), and removeItem().

Here is an example of using session storage to store and retrieve data:

sessionStorage.setItem('userName', 'John');
const userName = sessionStorage.getItem('userName');
console.log(userName); // Output: "John"

In this example, the setItem() method is used to store a value in session storage, and the getItem() method is used to retrieve the value. The data stored in session storage is available only during the current session, and is deleted when the user closes the browser or ends the browsing session.

Session storage can be a useful tool for storing data on the client-side and for maintaining state across multiple pages or requests within a single session. It can help you to build more responsive and interactive web applications, and to create a more seamless user experience.

Local Storage

Local storage is a client-side storage mechanism that allows you to store data on the client-side, so that it can be accessed and modified by your web application, even after the page has been refreshed or the browser has been closed and reopened.

Local storage is useful for storing data that needs to be persistent, such as user preferences or settings, or data that needs to be available across multiple sessions, such as authentication tokens. It can be accessed and modified using the localStorage object, which provides methods for storing and retrieving data, such as setItem(), getItem(), and removeItem().

Here is an example of using local storage to store and retrieve data:

localStorage.setItem('userName', 'John');
const userName = localStorage.getItem('userName');
console.log(userName); // Output: "John"

In this example, the setItem() method is used to store a value in local storage, and the getItem() method is used to retrieve the value. The data stored in local storage is available even after the page has been refreshed or the browser has been closed and reopened.