-1

for some reason when I try to run this code, it says

error taxableIncome is not defined

and/or

y is not defined

let taxableIncome = 80000;
if(taxableIncome >37000 && taxableIncome <80001);{
    const y = taxableIncome - 37000;
}
console.log(y);
console.log(taxableIncome);

I can't seem to figure it out. Any help would be appreciated! Thanks! (Sorry if this is a stupid question, I learnt basic JavaScript yesterday :/) Code

Vahid Farahmandian
  • 6,081
  • 7
  • 42
  • 62
  • 3
    Avoid posting screenshots of your code and just copy and paste it into the body of your questions. It makes it much easier to take a look at it and help out. – theJuls Jul 21 '18 at 12:28

6 Answers6

1

var is scoped to the function.

let and const are scoped to the block

That means that y is scoped to the if. Had you used var, this would not have occurred.

You will probably want to use let instead of const -- and then you can declare the variable outside the if, but assign to it inside the if.

You can't keep it a const, sadly, because you're changing its value later (just two lines later, but that doesn't matter).

Here is what that looks like:

let taxableIncome = 80000;
let y;
if(taxableIncome >37000 && taxableIncome <80001) {
    y = taxableIncome - 37000;
}
console.log(y);
console.log(taxableIncome);

In the above I also remove a semicolon just after the if's condition, but before it's opening {.


I might note this: it might not be sensible to try and get the value of a variable if you don't even know if it's been set. It all depends on the taxableIncome, and so I would add any code that relies on the variable y having a value to be inside the if as well -- and that will solve your problem too!

Max
  • 1,325
  • 9
  • 20
0

you have just use the const inside the if function so when the if has been finished it will not declare on the main scope, try to avoid this.

declare the const outside the if statement

var x;
if(1==1){
    x = 4;
}
console.log(x);
hod caspi
  • 826
  • 2
  • 10
  • 17
0

The issue is with scope. const has block scope, which means it is accessible only inside the block in which it is defined. In your piece of code, y is defined inside the if block. So you have to declare y outside the block.

let taxableIncome = 80000;
let y;
if(taxableIncome >37000 && taxableIncome <80001);{
 y = taxableIncome - 37000;
}
console.log(y);
console.log(taxableIncome);

Now y will be accessible. Check this link for more on scope in Javascript.

illiteratewriter
  • 4,155
  • 1
  • 23
  • 44
0

As your code currently stands, the y would be the one not defined. This is because you are declaring it in your if and attempting to console.log it outside of the if, where it is in a scope it hasn't been declared. This thread gives a good read on scopes in JS, I recommend you take a look.

I don't know in what circumstances you get the taxableIncome not being defined, but there are two ways to solve the y not being defined:

If you only use it within the if, it is absolutely fine to declare it in there, but your console.log and anything you do on it also has to moved into that if. So it should look like this:

let taxableIncome = 80000;
if(taxableIncome >37000 && taxableIncome <80001) {
    const y = taxableIncome - 37000;
    console.log(y)
    // anything else you do with the y must be in here
}
console.log(taxableIncome);

The more flexible solution and the one you probably want is to simply declare the y before the if. Of course since you are setting a value to it later on, it will have to be a let instead of a const.

let taxableIncome = 80000;
let y;
if(taxableIncome >37000 && taxableIncome <80001) {
    y = taxableIncome - 37000;
}
console.log(y);
console.log(taxableIncome);

Also, in another note, you do not need the semicolon after the if statement.

theJuls
  • 6,788
  • 14
  • 73
  • 160
0

You have some bugs in your code that you need to solve them:

  1. You have defined y inside the if block. so this variable will be only available inside this block, and outside of this block you will get undefined error, because there is no y defined in that scope;
  2. You have put ; at the end of the if condition check section(Line 2). This is not naturally an error but seems to be a bad practice

So you need to rewrite your code as below:

let taxableIncome = 80000;
let y = 0;
if(taxableIncome >37000 && taxableIncome <80001){
    y = taxableIncome - 37000;
}
console.log(y);
console.log(taxableIncome);

As you mentioned that you are rookie and newcomer to JavaScript, I think you need to read more about the differences between let and var too, so that you can use them wisely

Vahid Farahmandian
  • 6,081
  • 7
  • 42
  • 62
0

You are getting error because y is not in scope outside the if condition block. Correct code should be like this:

        let taxableIncome = 80000;
        if (taxableIncome > 37000 && taxableIncome < 80001); {
            const y = taxableIncome - 37000;
            console.log(y);
        }
        console.log(taxableIncome);

Do you have values that you don't want changed or that will never change? Like the value of Pi(3.14), they can be assigned to const in JavaScript. The value of a const cannot be changed through re-assignment and if you attempt to do so it will throw an error.

Parag
  • 38
  • 8