As you can see in the following image, I get an error stating that trd_sntnc is not defined, so the return statement does not run.
However, if you look at the red arrows I have drawn, trd_sntnc is clearly defined and top part of the if statement is clearly running (as evidenced by the console.logt bit of code which shows in the console
It is just before, yes, but the let is being defined in that if block…so it only exists within that if. As soon as your code exits those {...}, it’s out of scope.
It lives in the block of code you’ve put it in, not outside of it. JS has what’s called lexical scoping
{
let example = 1;
}
console.log(example)
// error
let example;
{
example = 1
}
console.log(example)
// not an error
The blocks of code can see variables defined outside them.
But the code outside a given block can’t see variables that are only defined in that block.
This means it’s much easier to avoid errors caused by accidentally overwriting variables when you have some piece of self-contained code inside a block in a specific part of your program.
hm… I would advice to initialice the variable before the if-else and only change the value inside.
Also if you are logging values anyway, try loging trd_sntnc within the if-else to see if that at least works.
Also there is some marking below the name, hovering over it with the mouse might give some failure message.
Personally I think the interpreter might just cause some trouble because you declare the variable twice. Like maybe it’s only taking the second let into account, but then because you don’t go into the else, it’s never actually created?
Interpreters can be a bit more tricky and I am no expert on them. But they are reading the entire code before executing anything and thus certain commands actually cause the interpreter to do actions in the middle of the code, before starting at the top. So it’s not actually as if it just goes from top to bottom. Hence only declaring the variable once might be the solution - plus it’s good practice to not have declerations spread out across the code.