# Where to use let and where to use var in a function node?

**URL:** https://discourse.nodered.org/t/where-to-use-let-and-where-to-use-var-in-a-function-node/33174
**Category:** General
**Created:** [22 September 2020 10:00 UTC](https://discourse.nodered.org/t/where-to-use-let-and-where-to-use-var-in-a-function-node/33174 "2020-09-22T10:00:25Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)
#### Post date: [22 September 2020 10:00 UTC](https://discourse.nodered.org/t/where-to-use-let-and-where-to-use-var-in-a-function-node/33174/1 "2020-09-22T10:00:25Z")

</div>

when would you recommend to use the let versa the var statement ?  
What is the scope of a function node?

Have a look at the code. If I replace all var with let I get an "already declared error"

```auto
var nodes = flow.get("nodes");

var node = nodes.find(n => n.nodeid == msg.payload.nodeid);

var value = node.values.find(v => v.value_id == msg.payload.value.value_id);

value.value = msg.payload.value.value;

msg.payload = value;
msg.topic = "value_changed";

return msg;
```

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [22 September 2020 10:07 UTC](https://discourse.nodered.org/t/where-to-use-let-and-where-to-use-var-in-a-function-node/33174/2 "2020-09-22T10:07:50Z")

</div>

you get error as `node` is already defined (e.g. `node.send()` is a function in the `node` object already defined in the function vm)

The fact this error occurs when you use `let` is a good demonstration of how `let` and `const` can help you find bugs.

EDIT...  
It is definitely preferable to use let and const over var.

---

<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: [22 September 2020 10:21 UTC](https://discourse.nodered.org/t/where-to-use-let-and-where-to-use-var-in-a-function-node/33174/3 "2020-09-22T10:21:18Z")

</div>

I agree, don't use var unless you have a good reason.

---

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [22 September 2020 11:15 UTC](https://discourse.nodered.org/t/where-to-use-let-and-where-to-use-var-in-a-function-node/33174/4 "2020-09-22T11:15:50Z")

</div>

> [@Colin](#):
>
> unless you have a good reason

And here is an example and explanation, something to think about (I definitely felt I sometimes needed var)

[let - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#:~:text=let%20allows%20you%20to%20declare,function%20regardless%20of%20block%20scope).

---

<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: [22 September 2020 11:25 UTC](https://discourse.nodered.org/t/where-to-use-let-and-where-to-use-var-in-a-function-node/33174/5 "2020-09-22T11:25:12Z")

</div>

Should that link have taken me to a specific example where var is required? It didn't.

---

<div class="post-metadata">

### Author: ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)
#### Post date: [22 September 2020 11:30 UTC](https://discourse.nodered.org/t/where-to-use-let-and-where-to-use-var-in-a-function-node/33174/6 "2020-09-22T11:30:13Z")

</div>

thanks for your help.  
this is how it works:

```auto
let nodes = flow.get("nodes");

let actual_node = nodes.find(n => n.nodeid == msg.payload.nodeid);

let value = actual_node.values.find(v => v.value_id == msg.payload.value.value_id);

value.value = msg.payload.value.value;

msg.payload = value;
msg.topic = "value_changed";

return msg;
```

---

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [22 September 2020 11:36 UTC](https://discourse.nodered.org/t/where-to-use-let-and-where-to-use-var-in-a-function-node/33174/7 "2020-09-22T11:36:07Z")

</div>

> [@Colin](#):
>
> Should that link have taken me to a specific example where var is required?

Not really. But a better explanation of the differences

---

<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: [22 September 2020 13:05 UTC](https://discourse.nodered.org/t/where-to-use-let-and-where-to-use-var-in-a-function-node/33174/8 "2020-09-22T13:05:22Z")

</div>

I think those can all be `const` as you only assign each reference once. You might think that `value` cannot be const as you write to value.value, but the variable `value` is, effectively, a pointer to a structure in memory, and that pointer is not changed when changing the contents of the object, so writing to value.value does not change the variable itself.  
In theory using const may allow the interpreter/compiler to generate more efficient code, but whether that is actually the case in the node-red environment I don't know. It does stop one accidentally overwriting a variable that should not be overwritten.

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [22 September 2020 21:16 UTC](https://discourse.nodered.org/t/where-to-use-let-and-where-to-use-var-in-a-function-node/33174/9 "2020-09-22T21:16:33Z")

</div>

> [@Colin](#):
>
> In theory using const may allow the interpreter/compiler to generate more efficient code, but whether that is actually the case in the node-red environment I don't know. It does stop one accidentally overwriting a variable that should not be overwritten.

I believe in practise it's more about the latter. I assume most function nodes are quite short so it might not be very likely to catch any errors. But I tend to also use `const` when possible out of the habit of it being the recommended practise for bigger code bases which I encounter daily at work.

---

<div class="post-metadata">

### Author: ![kevinGodell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kevingodell/32/27040_2.png) [@kevinGodell](https://discourse.nodered.org/u/kevinGodell)
#### Post date: [22 September 2020 23:17 UTC](https://discourse.nodered.org/t/where-to-use-let-and-where-to-use-var-in-a-function-node/33174/10 "2020-09-22T23:17:10Z")

</div>

`const` everything, unless you have to use `let`. only use `var` if you travel back in time. 🤓

---

<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: [21 November 2020 23:17 UTC](https://discourse.nodered.org/t/where-to-use-let-and-where-to-use-var-in-a-function-node/33174/11 "2020-11-21T23:17:11Z")

</div>

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