Troublemaker203
Troublemaker203

Reputation: 21

Check LocalStorage Value

I want to check if a special value is in the key like:

    if(localStorage.getItem('choice') == "simple")
    {       
        arrayname= simple.slice(); 

    }        
    else if (localStorage.getItem('choice') == "middle")
    {
        arrayname= middle.slice();

    }        
    else
    {
        arrayname= difficult.slice();

    }

Hope someone can help me.

Upvotes: 0

Views: 381

Answers (1)

Jonast92
Jonast92

Reputation: 4967

Let's dive into html5:

Like other JavaScript objects, you can treat the localStorage object as an associative array. Instead of using the getItem() and setItem() methods, you can simply use square brackets. For example, this snippet of code:

var foo = localStorage.getItem("bar");
// ...
localStorage.setItem("bar", foo);

…could be rewritten to use square bracket syntax instead:

var foo = localStorage["bar"];
// ...
localStorage["bar"] = foo;

So you can do something like this:

if(localStorage['choice'] == "simple")
{       
    //...
}        
else if (localStorage['choice'] == "middle")
{
    //...
}        
else
{
    //...
}

Upvotes: 1

Related Questions