Your problem here is the scope, you can find information about what the scope is here: https://www.w3schools.com/js/js_scope.asp
but in short, when you declare a variable inside a function, it is not accessible globally, you can access it only on that same function. If you want to access it from outside, you will need to declare it before;
Example:
let thisVariableIsGlobal = "hello world!";
(function yourFunctionName(){
let thisVariableIsLocal = "Quick test";
// You can edit thisVariableIsGlobal here and it will be applied globally, but don't redefine it using the var, let, or const keywords!
})()
console.log(thisVariableIsGlobal ) // Return hello world!
console.log(thisVariableIsLocal ) // Return undefined
or you can use the global object, for chrome for example you can access it through the "window" object, but this is not recommended.
Example
(function yourFunctionName(){
window['thisVariableIsGlobal'] = 'Hello world';
})()
console.log(thisVariableIsGlobal ) // Return hello world!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…