# Function nodes and \`let\` for variables

**URL:** <https://discourse.nodered.org/t/function-nodes-and-let-for-variables/76679>\
**Category:** General\
**Created:** [18 March 2023 00:33 UTC](https://discourse.nodered.org/t/function-nodes-and-let-for-variables/76679 "2023-03-18T00:33:09Z")\
**Posts on this page:** 12\
**Page:** 2

<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:** [18 March 2023 11:36 UTC](https://discourse.nodered.org/t/function-nodes-and-let-for-variables/76679/21 "2023-03-18T11:36:22Z")

</div>

Yes. If you think a variable won't change then use const. It can allow the javascript interpreter to optimise the code better. Also it prevents you accidentally overwriting it via a typo, for example.

If you add a line `x=7` to your example you will see the editor warn you.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [18 March 2023 11:42 UTC](https://discourse.nodered.org/t/function-nodes-and-let-for-variables/76679/22 "2023-03-18T11:42:51Z")

</div>

I'd maybe better call it closed now.

Thanks to all who helped.

I know it can seem frustrating from your sdie of thigns. you explain something and I don't understand.  
(Who's moved the keys on my keyboard?) 😉

But it is good that I now have a fairly good understanding of what it all means.

Now I had better get back to what I am trying to do.

Again: It is hard to say WHO was the ultimate help.  
If you are stuck with this problem, I'd suggest looking back at the demo flow I posted in reply 16 (I think)  
It shows 4 examples.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 March 2023 11:43 UTC](https://discourse.nodered.org/t/function-nodes-and-let-for-variables/76679/23 "2023-03-18T11:43:43Z")

</div>

```auto
const a_number =1;
const an_object = {};
const a_string = "asdfg";
const an_array =[];
a_number = 12; // error as changing value of const
a_string = "qwerty"; // error as changing value of const
an_object = 12; // an error because changing type of const
an_object = {"a":1}; // an error as assigning the var again.
an_object.a = 1; // no error as not assigning the var to an object, just adding a propperty
an_array = 12; // an error because changing type of const
an_array = [1]; // an error as assigning the var again.
an_array[0] = 1; // no error as not assigning the var to an array, just adding a propperty

```

---

<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:** [18 March 2023 11:59 UTC](https://discourse.nodered.org/t/function-nodes-and-let-for-variables/76679/24 "2023-03-18T11:59:12Z")

</div>

> [@E1cid](#):
>
> ```auto
> an_object.a = 1; // no error as not assigning the var to an object, just adding a propperty
> ...
> an_array[0] = 1; // no error as not assigning the var to an array, just adding a propperty
> 
> ```

I had hoped no-one would bring those up, in order to avoid further confusion.

I don't use const with objects or arrays as the effect is not intuitive.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [18 March 2023 12:04 UTC](https://discourse.nodered.org/t/function-nodes-and-let-for-variables/76679/25 "2023-03-18T12:04:47Z")

</div>

Well,

If the object or array is _static_......  
And is used for reference only.  
Say a lookup table/array - to call it.  
Like a table of tv stations that translates to their actual channel number.  
That would be a `const` - yes?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [18 March 2023 12:06 UTC](https://discourse.nodered.org/t/function-nodes-and-let-for-variables/76679/26 "2023-03-18T12:06:44Z")

</div>

> [@Colin](#):
>
> I don't use const with objects or arrays as the effect is not intuitive.

Sorry to wade in again. But to me, once you think of objects/arrays as _containers_, it becomes more intuitive. So, you can't do this:

```auto
const x = [1,2,3]
x = "fred"

```

Which saves a whole load of unexpected code errors.  
But you can do this

```auto
const x = {}
x.fred = "ooh!"

```

Because you are not _replacing_ x but simply adding to the container.

* * *

As for where to put variables - if you are using let and const - put them as close to where they are needed as possible!

That way, the JavaScript engine has an easier job of recovering the space when it is no longer needed.

But put var's at the top because that's where the JavaScript engine moves them to anyway at compile/run time.

> [@Trying\_to\_learn](#):
>
> That would be a `const` - yes?

Nope 🙂 see above. You are simply telling JavaScript that the object cannot be REPLACED. But it CAN be AMENDED.

* * *

The x variable here can be garbage collected at the end of the code-block if not before:

```auto
if (something === "something") {
   const x = 'hello'
   node.warn(x)
}
// ... do more stuff but x is no longer needed

```

Whereas it cannot be recovered in this code

```auto
if (something === "something") {
   var x = 'hello'
   node.warn(x)
}
// ... do more stuff but x is no longer needed

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [18 March 2023 12:11 UTC](https://discourse.nodered.org/t/function-nodes-and-let-for-variables/76679/27 "2023-03-18T12:11:00Z")

</div>

> [@TotallyInformation](#):
>
> Nope 🙂 see above. You are simply telling JavaScript that the object cannot be REPLACED. But it CAN be AMENDED.

I bow to your knowledge.

I was (probably) over simplifying how I see it.  
Which is not good.  
But at my level I am wanting to take baby steps.

Ok, it can be _amended_ also.

```auto
const x = {}
x.fred = "ooh!"

```

Over my head - sort of.  
So you are making x into an array. And the _pointer_ is called `fred` and it's value is "ooh!"  
`fred` seems a strange _pointer_. But I guess it isn't impossible.

_I'm off to bed.  
I lost 4 hours (there abouts) today with code and it all seeming to work then failing badly.  
I think I've done enough today._

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [18 March 2023 12:27 UTC](https://discourse.nodered.org/t/function-nodes-and-let-for-variables/76679/28 "2023-03-18T12:27:04Z")

</div>

> [@Trying\_to\_learn](#):
>
> So you are making x into an array. And the _pointer_ is called `fred` and it's value is "ooh!"  
> `fred` seems a strange _pointer_. But I guess it isn't impossible.

I'm making x into an OBJECT in that instance and the correct term rather than "pointer" is "property" but basically yes. And I often use "fred" and "jim" along with "foo" and "bah" - they are easy to type 😁

> [@Trying\_to\_learn](#):
>
> I think I've done enough today.

For sure, you should always listen to what your body is telling you.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [18 March 2023 22:09 UTC](https://discourse.nodered.org/t/function-nodes-and-let-for-variables/76679/29 "2023-03-18T22:09:50Z")

</div>

As much as I would like to not keep dragging this on:

I just found this interesting read.  
I am only about at the first/second page, but it talks about variables.

> **[The X Macro: A Historic Preprocessor Hack](https://hackaday.com/2023/03/16/the-x-macro-a-historic-preprocessor-hack/)**
>
> If we told you that a C preprocessor hack dated back to 1968, you’d be within your rights to remind us that C didn’t exist in 1968. However, assemblers with preprocessors did, and where…

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [19 March 2023 00:18 UTC](https://discourse.nodered.org/t/function-nodes-and-let-for-variables/76679/30 "2023-03-19T00:18:09Z")

</div>

Lord! I thought you didn't want to be confused! The nightmare that is C/C++ is like Alice in Wonderland compared to JavaScript (which is bad enough, lets be honest).

- [Storing the information you need — Variables - Learn web development | MDN (mozilla.org)](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Variables)
- [JavaScript Var vs. Let vs. Const. | JavaScript in Plain English](https://javascript.plainenglish.io/javascript-var-vs-let-vs-const-a-comprehensive-guide-f4aa817c5340)
- [Difference between var, let and const keywords in JavaScript - GeeksforGeeks](https://www.geeksforgeeks.org/difference-between-var-let-and-const-keywords-in-javascript/)
- [The Difference of “var” vs “let” vs “const” in Javascript | by Megan Lo | The Startup | Medium](https://medium.com/swlh/the-difference-of-var-vs-let-vs-const-in-javascript-abe37e214d66)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [19 March 2023 05:56 UTC](https://discourse.nodered.org/t/function-nodes-and-let-for-variables/76679/31 "2023-03-19T05:56:30Z")

</div>

Sorry.

I just stumbled on it and _thought_ it was applicable.

---

<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:** [2 April 2023 05:56 UTC](https://discourse.nodered.org/t/function-nodes-and-let-for-variables/76679/32 "2023-04-02T05:56:58Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/function-nodes-and-let-for-variables/76679.md?page=1)
