Session storage keys such as getItem, setItem and clear are retrieved when looping through all sessionStorage keys?

475 views Asked by At

In my web app, I store a few key value pairs in the browser sessionStorage using javascript. The issue I am experienceing is when looping through all keys in SessionStorage, I am getting (presumeably) system keys such as key, getItem, setItem, removeItem, clear. Using chrome dev tools, I can confirm that these keys do NOT exist in the Key Value table under Session Storage.

For Example:
In sessionStorage, there are 5 keys, for demo purposes named key_1 to key_5.

Using the following code:

sessionStorage.clear();
sessionStorage.setItem("key_1","test_1");
sessionStorage.setItem("key_2","test_2");
sessionStorage.setItem("key_3","test_3");
sessionStorage.setItem("key_4","test_4");
sessionStorage.setItem("key_5","test_5");
var seshString = "";
for (var key in sessionStorage){
 console.log(key);
 if(key != "key_2" && key != "key_3") {
  seshString += "&" + key + "=" + sessionStorage[key];
 }
}
console.log("Loop Complete");
console.log(seshString);

I get the following: key_1 key_2 key_3 key_4 key_5 length key getItem setItem removeItem clear Loop Complete &key_1=test_1&key_4=test_4&key_5=test_5&length=5&key=function key() { [native code] }&getItem=function getItem() { [native code] }&setItem=function setItem() { [native code] }&removeItem=function removeItem() { [native code] }&clear=function clear() { [native code] } Where are the extra sessionStorage keys coming from?

0

There are 0 answers