0

How to get a local variabel to be set into a global one and use it in a secondary function? function 1 will be called in HTML initially then function 2 will be called...

function test1(){
  var test1 = 1;
}

function test2(){
  var test2 = test1 + 3;
}
Ted
  • 107
  • 5
  • 1
    You don't. Declare the variable as global in the first place. – Bergi Jan 15 '23 at 20:57
  • 1
    Can you show us where/when (and in what order) you are calling `test1()` and `test2()`? – Bergi Jan 15 '23 at 20:58
  • 3
    Does this answer your question? [Define a global variable in a JavaScript function](https://stackoverflow.com/questions/5786851/define-a-global-variable-in-a-javascript-function) – Axiumin_ Jan 15 '23 at 21:00
  • 1
    "*function 1 will be called in HTML initially then function 2 will be called...*" - still not quite clear. How often are they going to be called? Why not just use a single function, where a local variable works? Why not declare the variable as global in the first place if a global variable is really what you want? – Bergi Jan 15 '23 at 21:04

0 Answers0