Web Storage API | SoloLearn: Learn to code for FREE!


Web Storage API

I want to know how to store data that are assigned to variable wrapped in functions so that even outside the function the data will be used..🤷 https://code.sololearn.com/W8Jqlez8Q46D/?ref=app

5 Answers

New Answer


Pude [#BornofGod]😇🌹😘🇳🇬 I think you can return all those important variables. (Edit) ex: function add(a,b){ return a+b; } var result = add(2,3); //result = 5 /**** or *****/ console.log(add(7,4)); //11


If you use a global var , you will be able to store data and reuse it everywhere . Now your code is working : https://code.sololearn.com/WRPPHvW9ynT5/?ref=app


You can have a variable be declared in the global space, and assign anything to it anywhere. But how web storage or that linked code be related to your question? Only attach relevant links please.



If you want to use web storage API then it's great to use. You can go with two options. 1. Local storage - will be there for that web app even if you close the browser (removed when cache /storage cleared for that web app) 2. Session storage - available only when app is running (if you close the tab, it's gone) Both have same functions. 1. localStorage.getItem("item_name"); 2. localStorage.setItem("key", "value"); Or 1. sessionStorage.getItem("item_name"); 2. sessionStorage.setItem("key", "value"); Remeber, when you're storing an object or JSON type of data. Use JSON.stringify("json_object") to store And to fetch use, JSON.parse("value"); Ex. 1. let a = JSON.parse(localStorage.getItem("item_name")); 2. localStorage.setItem("key", JSON.stringify("json_object")); I hope you get the idea.