Can't define variable into an "if" scope

Hi, yesterday I've became a bit crazy knowing why the code didn't work, in particular I was defining the desidered output node's object message into an if code, the deploy didn't notify an error but the message could't be output, like the code inside the "if" { code } was not executed. It was executed but the "let" command coudn't be written there, it should be outside the "if".
I would suggest to help users, checking this and marking it as an error.

Struggling to understand the situation here Giovanni.

Perhaps you could show us a function code to illustrate the point?

Ok, for example:

let output = {};
if (msg.payload) {output = {works:true};}
return output;
if (msg.payload) {let output = {works:false};} else {let output = {works:false};}
return output;

the second example oesn't return errors, the code is executed but message output is not defined/created

That is perfectly legal javascript, but when you use let inside a {} block then it declares a new variable local to the block. Inside the block the original variable is not accessible. Your code should be

if (msg.payload) {
   output = {works:false};
} else {
  output = {works:false};
}
return output;

[Edit] it seems a bit odd to have the same code in the if and in the else.
Also, as @jbudd has noted, you need let output before the if

For me this code is not flagged as an error in the editor, but it does give a run-time error

if (msg.payload) {
    let output = {works:false};
} 
else {
    let output = {works:false};
}
return output;

ReferenceError: ReferenceError: output is not defined (line 7, col 1)

(Because of course output only has a value within the two sets of braces where it is declared.)

I suppose one might hope that the code editor was smart enough to check the variable scope, but apparently it's not - unless there is a setting somewhere for this.

It is a bit uncomfortable to have to declare let output or let output = {} somewhere at the top, especially if you come from a language where variables don't need to be declared.

In addition to jbudd message, you can try with:

If you install nrlint from the .node-red folder
npm install nrlint
then the code does generate warnings.

Thank you everyone, now I've understood. Yes it has perfectly sense to declare variables only on particular {scope}, but I dubt it's really useful, anyway that's it.
I didn't now that before, better for me install nrlint to avoid such misunderstandings in future