Local Storage

Quick, Simple & Easy Function for storing data in localStorage

by ConnorM

JavaScript

var storage = function ( name, value ) {
    if(value) {
        value = typeof value === 'string' ? value : JSON.stringify( value );
        localStorage.setItem( name, value );
        return true;
    } else {
        var result = localStorage.getItem(name); 
        try { return JSON.parse( result ); } 
        catch ( string ) { return result; }
    }
};

    storage('item', {firstName: 'Connor', lastName: 'Miles'}); // Store Object 
    storage('items', 'Many Items'); // Store String

    console.log( storage('item'), ' <= Object || String => ', storage('items') );