-4

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

Omnni
  • 3
  • 4
  • They are undefined because they are being used in the wrong [scope](https://developer.mozilla.org/en-US/docs/Glossary/Scope). You can declare the variables [globally](https://developer.mozilla.org/en-US/docs/Glossary/Global_variable), or pass them as [arguments](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions) to the functions that need them – Cjmarkham May 29 '23 at 16:52
  • `x` is undefined because initially `level` is undefined as well. So `x` is never assigned a value. – Đinh Carabus May 29 '23 at 16:54
  • 1
    don' use `var` anymore. it's dangerously outdated. learn how to use `const` and `let` and enjoy – Alexander Nenashev May 29 '23 at 16:56
  • level is not undefined – Omnni May 29 '23 at 16:56
  • I could put them globaly but if level 2, y will never be used.Can I make something like I posted above so variables like x be defined only if level == 1 and and y only if level == 2. – Omnni May 29 '23 at 16:57
  • 2
    `level` does start off `undefined`. `let level` isn't assigning a value so it's `undefined` – Cjmarkham May 29 '23 at 16:58
  • _"My goal is to declare as minimum as possible variables I can"_: maybe use an object. – Andy May 29 '23 at 17:00
  • it is assigned in find level – Omnni May 29 '23 at 17:00
  • 1
    Understand that `level = 1` happens *after* `if (level == 1)`! – deceze May 29 '23 at 17:01
  • @Omnni, yes but `findLevel` runs after the if/else. So `var y = 2` is executed and `x` stays `undefined`. – Đinh Carabus May 29 '23 at 17:01
  • 1
    I'd recommend reading up on the [order of execution](https://medium.com/@marcellamaki/a-brief-overview-of-order-of-execution-in-javascript-e28744aa9479) of JavaScript – Cjmarkham May 29 '23 at 17:02

1 Answers1

-5

The variables declared in the if statement, x and y, are in that scope, which means they are not accessible from the global or another scope. Like you did with level, declare x and y at the beginning as such:

let level, x, y;

This way, if level is NOT 1, x will be declared, and if it IS 1, then y will be declared.

Hermanboxcar
  • 418
  • 1
  • 13