My problem lies in defining a variable inside an if statement and using it in a function like:
Why is this not working? Are there any workarounds to the issue?My goal is to use as little variables as possible
let level
function findLevel(){
if (document.title.indexOf("PageTitle") != -1) {
level = 1
}else{
level = 2
}
}
if (level == 1) {
var x = 1
//more variables like x that are going to be used in more than one functions..
}else{
var y = 2
//more variables like y that are going to be used in more than one functions..
}
function functionName() {
console.log(level) // prints 1
console.log(x) // prints undefined
// ...some Work...
}
function functionName2(){
//also need x or y
}
//more functions like this
window.onload =function(){
findLevel(); functionName();
}
My goal is to declare as minimum as possible variables I can