Let’s say we have two files - A.js and B.js.
I had this challenge where I had to find some way to obtain a value of a variable from one A.js file and use the value of variable in B.js file. I also had the limitation where I couldn’t merge A.js with B.js. It wasn’t practical to make a third file placing the common parts of codes from A.js and B.js into it.
Then, I figured out something called Local Storage. With local storage, web applications can store data locally within the user's browser.
Earlier, the application data was stored on cookies in every server request, provided the storage limit of cookies is small as compared to the storage limit provided by local Storage
Local storage is more secure, and large amounts of data can be stored locally without affecting website performance, and the information is never transferred to the Servers.
All pages can store and access the data stored in Local Storages.
The implementation and use of Local Storage is shown below.
- My A.js and B.js files have 2 variables named variableOne and VariableTwo with values “valueOne” and “valueTwo” respectively.
- A.js var variableOne = "valueOne";
- B.js var variableTwo;
- We need to get the value from variableOne from A.js and set it in variableTwo in B.js.
First, we will store the value of variable “variableOne” from A.js in local storage.(Below is the code written in A.js)
- localStorage.setItem("vOneLocalStorage", variableOne);
- We will obtain the value of variable “variableOne” from Local Storage.
In the localStorage, the value is stored in the variable “vOneLocalStorage”.So, the Get method includes mentioning the localStorage variable, as shown below.(Below is the code on B.js)
- var vOneLS = localStorage.getItem("vOneLocalStorage ");

Join the conversation! Your thoughts help the community grow.