# Can't define variable into an "if" scope

**URL:** <https://discourse.nodered.org/t/cant-define-variable-into-an-if-scope/92688>\
**Category:** General\
**Created:** [26 October 2024 11:37 UTC](https://discourse.nodered.org/t/cant-define-variable-into-an-if-scope/92688 "2024-10-26T11:37:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![GiovanniG](https://avatars.discourse-cdn.com/v4/letter/g/ecd19e/32.png) [@GiovanniG](https://discourse.nodered.org/u/GiovanniG)\
**Post date:** [26 October 2024 11:37 UTC](https://discourse.nodered.org/t/cant-define-variable-into-an-if-scope/92688/1 "2024-10-26T11:37:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [26 October 2024 11:47 UTC](https://discourse.nodered.org/t/cant-define-variable-into-an-if-scope/92688/2 "2024-10-26T11:47:56Z")

</div>

Struggling to understand the situation here Giovanni.

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

---

<div class="post-metadata">

**Author:** ![GiovanniG](https://avatars.discourse-cdn.com/v4/letter/g/ecd19e/32.png) [@GiovanniG](https://discourse.nodered.org/u/GiovanniG)\
**Post date:** [26 October 2024 11:54 UTC](https://discourse.nodered.org/t/cant-define-variable-into-an-if-scope/92688/3 "2024-10-26T11:54:06Z")

</div>

Ok, for example:

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

```

```auto
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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 October 2024 12:23 UTC](https://discourse.nodered.org/t/cant-define-variable-into-an-if-scope/92688/4 "2024-10-26T12:23:19Z")

</div>

> [@GiovanniG](#):
>
> ```auto
> if (msg.payload) {let output = {works:false};} else {let output = {works:false};}
> return output;
> 
> ```

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

```auto
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`

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [26 October 2024 12:28 UTC](https://discourse.nodered.org/t/cant-define-variable-into-an-if-scope/92688/5 "2024-10-26T12:28:55Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [26 October 2024 12:32 UTC](https://discourse.nodered.org/t/cant-define-variable-into-an-if-scope/92688/6 "2024-10-26T12:32:13Z")

</div>

In addition to jbudd message, you can try with:

> **[let - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let)**
>
> The let declaration declares re-assignable, block-scoped local variables, optionally initializing each to a value.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 October 2024 12:38 UTC](https://discourse.nodered.org/t/cant-define-variable-into-an-if-scope/92688/7 "2024-10-26T12:38:59Z")

</div>

> [@jbudd](#):
>
> one might hope that the code editor was smart enough to check the variable scope,

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

---

<div class="post-metadata">

**Author:** ![GiovanniG](https://avatars.discourse-cdn.com/v4/letter/g/ecd19e/32.png) [@GiovanniG](https://discourse.nodered.org/u/GiovanniG)\
**Post date:** [26 October 2024 12:52 UTC](https://discourse.nodered.org/t/cant-define-variable-into-an-if-scope/92688/8 "2024-10-26T12:52:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [24 January 2025 12:52 UTC](https://discourse.nodered.org/t/cant-define-variable-into-an-if-scope/92688/9 "2025-01-24T12:52:32Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
