Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Local storage in JavaScript is a feature provided by the Web Storage API that allows you to store data on the client-side (i.e., in the user's browser) in a way that persists even when the browser is closed and reopened. This is particularly useful for storing small amounts of data, like user preferences, application settings, or even basic app data. Here’s a breakdown of how it works and some basic commands.

Basic Concepts LocalStorage: Stores data with no expiration date. Data will persist even if the browser is closed and reopened. SessionStorage: Stores data for one session (i.e., the data is lost when the tab is closed). Using Local Storage The localStorage object provides several methods to work with local storage data.

Set Item: Save data to local storage.

localStorage.setItem('key', 'value'); key: The name of the data item you want to store. value: The data you want to store. (Must be a string; if it's an object, use JSON). Example:

localStorage.setItem('username', 'JohnDoe'); Get Item: Retrieve data from local storage.

const value = localStorage.getItem('key'); Example:

const username = localStorage.getItem('username'); console.log(username); // Outputs: JohnDoe Remove Item: Delete an item from local storage.

localStorage.removeItem('key'); Example:

localStorage.removeItem('username'); Clear Storage: Remove all data from local storage.

localStorage.clear(); Check Storage Length: Get the number of items in local storage.

const length = localStorage.length; Storing and Retrieving Objects Since localStorage only supports strings, objects must be converted using JSON.stringify() to store and JSON.parse() to retrieve.

Example:

// Storing an object const user = { name: 'John Doe', age: 30 }; localStorage.setItem('user', JSON.stringify(user));

// Retrieving an object const storedUser = JSON.parse(localStorage.getItem('user')); console.log(storedUser.name); // Outputs: John Doe Use Cases Remember user preferences (e.g., dark mode). Temporary caching of small amounts of data. Storing authentication tokens (but note that localStorage has security considerations, so use with caution for sensitive data). Limitations Storage capacity is generally limited to about 5–10MB depending on the browser. Data is stored as strings, so objects need to be stringified. Security concerns: Data in localStorage is accessible via JavaScript, making it susceptible to XSS (Cross-Site Scripting) attacks if not handled securely. Avoid storing sensitive data, such as passwords, directly.

About

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages