The Best and easiest solution to your question
Hi. I had researched a lot about the question you are asking. I watched many tutorials, but they were really complicated. So once, I was working on a project. I didn't realise, but I used a variable created/edited in a function in another function. It was really simple. You need to create Local Storage to do this. The localStorage and sessionStorage properties allow to save key/value pairs in a web browser. It is Supported in almost every famous or known web browser like Google Chrome, Firefox, Opera, safari, Microsoft Edge etc.
Note: if you click Run code snippet button, the code won't work because Stack Overflow doesn't support Local Storage. So, go to another text editor like notepad, run the code, and see the result on any Browser (mentioned above). It will work.
// Creates a variable
var max = "Nothing";
function btn1() {
// Makes a change to max variable
var max = "Max variable changed";
// Creates Local Storage on browser
var setLocalStorage = localStorage.setItem("max", max);
}
function btn2() {
var getLocalStorage = localStorage.getItem("max");
document.ById("text").innerHTML = getLocalStorage;
}
<html>
<body>
<p id="text"> Nothing </p>
<button onclick="btn1()"> Make change </button>
<button onclick="btn2()"> See change </button>
</body>
</html>