Dwza Dwza - 1 year ago 83
Javascript Question

sessionstorage works but returns "undefined" in console. why?

I just build a jquery plugin to handle some data storages.
when i try to read from storage, the result is the read item data. fine for now....

when i add

sessionStorage.setItem('test','some data');

or remove


a item, i allways get a
in the

but it do what i it should do.

how can i get rid of the undefined, any ideas ???

what i tried was

var result = sessionStorage.setItem('test','some data');

thought this would print it to a var but it won't ^^

my code:

(function ($) {

$.storage = {

add: function(name,value){

if(typeof value === 'object' )
/** convert object to json **/
value = JSON.stringify(value);
/** add to storage **/

read: function(name){

/** read from storage **/
var item = sessionStorage.getItem(name);
try {
item = $.parseJSON(item);
return item;

remove: function(name){
/** delete from storage **/

clear: function(){
/** clear storage **/



I was trying the add storage in console... when i use it in my script it returns non! SORRY

Answer Source

setItem and removeItem doesn't return any value. Thus you are getting undefined. Also note removeItem only accept one parameter.

Reference, Here is function sign for above methods

setItem(key:string, data:string):void

This public method stores data via specified key. Please note that if key was already stored, it will overwrite it. Another thing to consider is that if we store a key, and via sessionStorage.key(0) we obtain that key, then we store another key/data and finally we store again the first key, sessionStorage.key(0) will return the second key/data stored, and not the first one.


This public method simply removes a key and related data from the storage.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download