Function nodes and `let` for variables

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.

1 Like

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?) :wink:

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.

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
1 Like

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.

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?

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:

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

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

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.

Nope :slight_smile: 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:

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

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

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.

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.

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 :grin:

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

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.

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).

1 Like

Sorry.

I just stumbled on it and thought it was applicable.

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