Although there are other problems in the mix, they aren't of concern at this stage.
Yes, I know this is probably a recipe for disaster, but please indulge me.
This node code is from within a subflow I made (a long time ago) and it works, as in: it does what I wanted it to at time of making it.
I have since realised there is a problem with it and so I want to modify it.
I open the function node and then the fun begins.
I have the newer editor (not the default one) and it supposedly helps with syntax, etc.
So status quo the function node isn't complaining and it does things correctly.
I open it and this is the code.
Even if I don't edit and click the OK button: the node shows the dreaded red triangle (error) on it.
Define all the variables to be used.
Assign their values in the next stage.
Yes, the way you did it is nicer, but I fear that I am not good enough to confidently do it that way yet.
In this example it is obvious what needs to be done, but when I am writing bigger sets of code I an trying to establish the structure so I define all the variables at the start then lower down I assign values to them.
That is better when the value being assigned can also be variable.
Again: This is probably a poor example, but I'm torn which way to go.
RE const, let, var...
My rule of thumb is: "Always use const, unless you cannot, then use let"
There are technical reasons why but lets just say, follow the rule & avoid the technicalities.
Personally, I don't like to use const on an object, where the contents of the object are later changed. I know that it is valid javascript, as a variable to which an object is assigned is effectively a pointer to the object, so saying const msg1 = {} is saying that msg1 is a constant pointer to an object, the object itself is not constant, so it is ok to assign a value to msg1.payload. I know that this can be confusing, as the expectation is that using const would be to create a constant object, so later assigning a value to a property of the object would not be allowed. Therefore, for objects, I always use let, just to avoid confusion.
I know - as I am sure you are aware - I've sort of been down this road before.
I am still not 100% clear on the mechanics of what constlet and var.
I've been shown the documentation and looked at the magic words but that is where things fail for me.
I am not by any means a programmer and am amazed by what I've done.
I'm not sure if it is my stubbornness or I actually am good at it. (Learning?)
At this stage I am trying to establish a structure on how I write things that then will become constant (no pun intended) when I write code.
Alas I missed a lot of learning to code as from about the late 90's to a few years back when I came here and joined the group in Node-Red I had not done any real programming.
Ok, a small bit in arduino, but that is even worse than my JS ability as it was done only a few times for jobs that needed to be done there and then.
JS is ongoing and I fear I am re-enforcing bad habits.
So I ask now and then to check what the more universally accepted ways are.
Probably just as confusing and standard as standards can be.