Because "Index" will not be set in localStorage
, and calling getIndex()
on a non-existant key returns null
. You're left with describeArray[null]
, which won't work (and will give you undefined
).
var index = localStorage.getItem("Index");
if (index === null) {
index = 0;
}
describe.innerText = describeArray[index];
... or you can just do var index = localStorage.getItem("Index") || 0;
(via short circuit evaluation)