Reading localStorage values via indexes

how to get data from localstorage in javascript
localstorage setitem object
localstorage.setitem not working
localstorage get all items
javascript save file to local storage
localstorage getitem
localstorage is not defined
clear localstorage

Double your localStorage read performance in Chrome using indexes ls.getItem[i] not keys ls.getItem('key')

Source: this tweet and this talk (slide 19) by some guys that wrote a couple of awesome JavaSciript libraries. I guess these guys know what they are talking about, so I'm wondering why this doesn't work for me:

localStorage.setItem(0, 'Hello world');
localStorage.getItem(0); // 'Hello World' – Works as expected
localStorage.getItem[0]; // undefined

Did I misunderstand something? According to that talk, the last method call should return 'Hello world' not undefined.

They've also linked to this JS Bin, but when reading the values via index there, I still get undefined.

Edit: I know how to properly read the indexes with localStorage, I'm asking about getItem with indexes. It seems the talk just suggests a wrong usage? Also, I wonder why 32 people retweeted that usage, without having tested it. Those things irritate me.

My name is Chris and I am the one responsible for the slide that is causing confusion. For the confusion, I apologize. The slide was somewhat of a joke; allow me to explain..

In the days leading up to our panel, a tweet was posted and retweeted about doubling your read performance of localStorage. The tweet is the one you referenced (Twitter). However, when I saw this tweet, I did a double take because the syntax looked very wrong to me. I tested the syntax only to discover the same thing that you did: this syntax is clearly not valid if you are trying to access values in localStorage.

On stage at SXSW, this myth was presented as a joke. Yes, you get double the performance from referencing number indexes on localStorage.getItem. However, all of the values are undefined.

Anyway, it appears that in the attempt to undo the spread of misinformation, I have managed to spread it further. Thanks for bringing the issue to our attention!

Arm yourself with the knowledge and power of localStorage in JavaScript applications with these five July 17, 2018 2 min read 566 Just as the name implies, this method allows you to store values in the localStorage object. keys and allows you to pass a number or index to localStorage to retrieve the name of the key. The localStorage and sessionStorage properties allow to save key/value pairs in a web browser. The localStorage object stores data with no expiration date. The data will not be deleted when the browser is closed, and will be available the next day, week, or year. The localStorage property is read-only. Tip: Also look at the sessionStorage


Is a function so you can't acces it using the array notation, instead you can use:

localStorage.setItem(0, 'foo');
localStorage[0]; // 'foo'

If you use a string key you can do this:

localStorage.setItem('foo', 'bar'); // 'bar'

localStorage and sessionStorage allow to save key/value pairs in the setItem​/getItem/removeItem ), but also allows access by index with  The read-only localStorage property allows you to access a Storage object for the Document 's origin; the stored data is saved across browser sessions. localStorage is similar to sessionStorage, except that while data stored in localStorage has no expiration time, data stored in sessionStorage gets cleared when the page session ends — that is, when the page is closed.

You just can't do that. The two proper ways to get an item are by getItem or by an index directly on localStorage; not by getting a property of getItem.

There's nothing in the documentation that mentions this feature, and it doesn't work on any browser I've tried, so I'm not sure where they got it from. Maybe it's just so exciting that it improves performance that they didn't bother to check if it worked?

In this article, we'll store data with cookies and LocalStorage and retrieve it. other websites from manipulating or reading our client data that's used by our domain. The browser creates an integer index for each item added to LocalStorage. 1 Reading localStorage values via indexes May 21 '12 1 Node.js, nodetuts tutorial - example code will run, timeout after a while,(Chunked HTTP encoding) Jul 1 '12 1 HTML BASE doesn't work properly in FF 13 and Chromium 18 on Ubuntu 12.04 Jul 1 '12

I think it should just be:


That certainly works in the most recent version of Chrome.

A global index needs to be allocated by a call to TlsAlloc( ). the index with a call to TlsSetValue( ) and can read the thread-local data with a call to TlsGetValue( ). In Listing 6.39, thread-local storage is used to hold the value passed into each  4 Reading localStorage values via indexes May 23 '12 2 Cannot use Cloudflare's Rocket Loader in manual mode with wp_enqueue_script Feb 29 '12 1 Google model-viewer - how to set max zoom?

I don't see how this could work:


getItem is a method and can't be accessed like that.

It is possible to access localStorage directly by the key though. It can be done like this:

console.log( localStorage[0] );
console.log( localStorage['test'] );

Whether or not it is faster than the methods, you'd have to test it for yourself.

jsFiddle here.

If the index does not exist, null is returned. Examples. The following function iterates over the local storage keys: function forEachKey(callback)  Integrating local storage. Now we're going to add a few more bits of functionality to the app. First, every time the form is submitted, the input value should be added to the localStorage as well as appear on the front end. We'll also want to loop through all the existing local storage items and display them at the top of the list.

Mar 18, 2019 · 4 min read This means the data stored in the browser will persist even after the browser window has been closed. to loop through keys and allows you pass a number or index to local storage to retrieve the name of the key. The complete guide to using localStorage in JavaScript apps July 17, 2018 2 min read 566 localStorage is a type of web storage that allows JavaScript sites and apps to store and access data right in the browser with no expiration date.

The key() method returns name of the key with the specified index. The key() Example. The same example, but using session storage instead of local storage. HTML5 local storage makes it possible to store values in the browser which can survive the browser session, just like cookies. Local storage also makes it possible to send events between browser

03/05/2020; 6 minutes to read Web storage (Local and Session storage) key/​values pairs; Indexed DB Use the Local Storage manager and Session Storage manager to inspect and manage the web storage for your page. To delete all the IndexedDB data stored for the current user in Microsoft Edge,  My retrievedData variable is storing the values returned by my local storage item whose key is quentinTarantino. This data is currently in the form of a string. This data is currently in the form of a string.

  • By the way, the presentation on Google Docs seems to be private.
  • @apsillers Maybe you need to be logged into Google to see it? I'm pretty sure it's not a private one.
  • Oh, I can see it now. Looks like it was one-time glitch. Anyway, I agree it's pretty surprising that they would make that mistake twice in two different contexts, but it is definitely incorrect.
  • Thanks for the clarification. The joke was hard to get for me because I only looked at the slides. :)