# JS, people still use var?

**URL:** https://discourse.nodered.org/t/js-people-still-use-var/92258
**Category:** General
**Created:** [9 October 2024 09:18 UTC](https://discourse.nodered.org/t/js-people-still-use-var/92258 "2024-10-09T09:18:33Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)
#### Post date: [9 October 2024 09:18 UTC](https://discourse.nodered.org/t/js-people-still-use-var/92258/1 "2024-10-09T09:18:33Z")

</div>

Looking at javascript examples and source code for node red palette packages, the use of "var" to declare variables is rampant. I can only see a few reasons for this:

1. People who write this code doesn't know any better.
2. Muscle memory, old/bad habits.
3. Ancient code not updated.

Bad code being so widespread will only cause fresh developers to make the same mistake, learning from the old.

This would easily be solved with a linter or even better, a formatter which auto-change to const/let on save.

Thoughts?

---

<div class="post-metadata">

### Author: ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)
#### Post date: [9 October 2024 09:21 UTC](https://discourse.nodered.org/t/js-people-still-use-var/92258/2 "2024-10-09T09:21:40Z")

</div>

Missing point 4. "If it's working, don't touch it."

---

<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: [9 October 2024 09:36 UTC](https://discourse.nodered.org/t/js-people-still-use-var/92258/3 "2024-10-09T09:36:28Z")

</div>

> [@ThingsTinkerer](#):
>
> Ancient code not updated.

> [@ThingsTinkerer](#):
>
> This would easily be solved

Maybe easy, but also time consuming. It requires someone to put the time in when there are other priorities.

---

<div class="post-metadata">

### Author: ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)
#### Post date: [9 October 2024 10:11 UTC](https://discourse.nodered.org/t/js-people-still-use-var/92258/4 "2024-10-09T10:11:23Z")

</div>

Admittedly those configs are a hassle, and time is long overdue for an industry standard to copy-paste from. Chatgpt or similar should also be able to make something fairly decent instantly. Best case it's a matter of spending 10 min setting up a project before writing code. A one-time job. If you have done so already, just copy to new projects. More often than not, you end up saving that invested time down the line.

---

<div class="post-metadata">

### Author: ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)
#### Post date: [9 October 2024 10:14 UTC](https://discourse.nodered.org/t/js-people-still-use-var/92258/5 "2024-10-09T10:14:33Z")

</div>

Haha don't touch it yeah, related to "ancient code not updated". You would have some extremely specific cases where changing "var" to "const"/"let" breaks something. Average node-red level is far below that level.

---

<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: [9 October 2024 10:30 UTC](https://discourse.nodered.org/t/js-people-still-use-var/92258/6 "2024-10-09T10:30:22Z")

</div>

> [@ThingsTinkerer](#):
>
> Best case it's a matter of spending 10 min setting up a project before writing code

Plus all the re-testing, release notes, pushing to github releasing to npm, etc. And for what purpose?

---

<div class="post-metadata">

### Author: ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)
#### Post date: [9 October 2024 12:20 UTC](https://discourse.nodered.org/t/js-people-still-use-var/92258/7 "2024-10-09T12:20:04Z")

</div>

For what purpose? You mean having a linter and formatter for your project? If you spent 10 min beforehand, you wouldn't need to do additional re-testing, release notes, push to github and npm release. If you do it afterwards, then yeah. Tests can be automated, so ideally that'll be no issue. Pushing to github takes literally seconds. No idea about the npm, probably automatable too? For changing var to let, my guess is those who used it unknowningly, it won't have any effect at all.

For var itself I get that you ask for purpose. But for general code quality, you think that has no value in itself?

---

<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: [9 October 2024 12:25 UTC](https://discourse.nodered.org/t/js-people-still-use-var/92258/8 "2024-10-09T12:25:10Z")

</div>

I have many older function nodes using var and many newer function nodes using let and const.  
As far as I know they all work fine.

If there was a tool to rewrite these old functions, what would I gain by using it?

---

<div class="post-metadata">

### Author: ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)
#### Post date: [9 October 2024 12:36 UTC](https://discourse.nodered.org/t/js-people-still-use-var/92258/9 "2024-10-09T12:36:58Z")

</div>

That'll be " Ancient code not updated." 😀 Cleaner code? Best-practice? You can write entire code on single line too if you want, still works fine. Yet using a formatter would be much better (with negligible cost in setup).

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [9 October 2024 13:08 UTC](https://discourse.nodered.org/t/js-people-still-use-var/92258/10 "2024-10-09T13:08:11Z")

</div>

Well I'm sure that since you think this should be done, you could start making changes and submit PR to have the code changed.

---

<div class="post-metadata">

### Author: ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)
#### Post date: [9 October 2024 13:08 UTC](https://discourse.nodered.org/t/js-people-still-use-var/92258/11 "2024-10-09T13:08:52Z")

</div>

You don't agree that var shouldn't be used? Instead of fixing it, it would be far more practical to set up a CI/CD pipeline to prevent it ever happening in the first place.

---

<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: [9 October 2024 13:26 UTC](https://discourse.nodered.org/t/js-people-still-use-var/92258/12 "2024-10-09T13:26:43Z")

</div>

Alas it would take much more than s/var/let/ to make my code resemble "best practice"

My question "what would I gain...?" was in fact a question and not the statement "I would gain nothing".  
"Cleaner code" really tells me nothing.

I feel that what is important is to be able to read and understand code months down the line.  
Actually I find "let" rather impairs understanding since I know other languages where let is an assignment not a declaration.

```auto
let someThing;

```

reads to me like a syntax error

I do try and use it though because people say it is in some mysterious way better.

---

<div class="post-metadata">

### Author: ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)
#### Post date: [9 October 2024 15:23 UTC](https://discourse.nodered.org/t/js-people-still-use-var/92258/13 "2024-10-09T15:23:32Z")

</div>

Not sure I want to spiral down a var vs const/let debate, as I thought the community was in agreement on the topic. But here is clearly some doubt! I don't remember working with any other language that use let as assignment, but probably stuff like kotlin, f#, swift and rust? However in js (and more so in ts), using var actually reads like a syntax error. I stumbled upon it here with node red recently, accidentally using var myself, as I spent more time working in typescript earlier and then, as mentioned, these sorts of issues are automatically handled (so never thought about it). Until stumbling across one of many bugs due to incorrect use of var. Which is hard to read, particularly when coming from other languages (let's say c#, c++, java or even python?).

Your code may have many other problems than this which would be best suitable for another topic. But if you don't care it hardly matters? What matters is when you start publishing your code for others to read, learn from or even maintain. That's what triggered this discussion.

---

<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: [9 October 2024 16:07 UTC](https://discourse.nodered.org/t/js-people-still-use-var/92258/14 "2024-10-09T16:07:46Z")

</div>

> [@ThingsTinkerer](#):
>
> For what purpose? You mean having a linter and formatter for your project? If you spent 10 min beforehand, you wouldn't need to do additional re-testing, release notes, push to github and npm release.

I meant for what purpose reworking and re-releasing my six year old code, which is currently working well? That is what you appeared to be suggesting.

> [@ThingsTinkerer](#):
>
> For var itself I get that you ask for purpose. But for general code quality, you think that has no value in itself?

I was absolutely not suggesting that new code (or old code that is being worked on anyway) should use `var`. I often point out to posters here that it is better to use `let`/`const`.

---

<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: [9 October 2024 16:11 UTC](https://discourse.nodered.org/t/js-people-still-use-var/92258/15 "2024-10-09T16:11:08Z")

</div>

> [@ThingsTinkerer](#):
>
> I don't remember working with any other language that use let as assignment

You are presumably to young to have experienced the joys of Basic. Lucky you!

---

<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: [9 October 2024 16:27 UTC](https://discourse.nodered.org/t/js-people-still-use-var/92258/16 "2024-10-09T16:27:36Z")

</div>

> [@jbudd](#):
>
> what would I gain...?

Try this for example

```auto
// declare a variable x with function wide scope
var x = 3
var y = 4
// some code
if (x != y) {
    // declare a variable local to this block
    var x = 0
    // some more code
}
// set payload to the function wide variable
msg.payload = x
return msg;

```

---

<div class="post-metadata">

### Author: ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)
#### Post date: [9 October 2024 16:31 UTC](https://discourse.nodered.org/t/js-people-still-use-var/92258/17 "2024-10-09T16:31:47Z")

</div>

Yeah agreed. This topic was not triggered by your old code hehe 😄 It is a very minor thing, but pops up regularly in code some publish for others to read, learn from or contribute on.

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [9 October 2024 16:36 UTC](https://discourse.nodered.org/t/js-people-still-use-var/92258/18 "2024-10-09T16:36:26Z")

</div>

> [@ThingsTinkerer](#):
>
> Bad code being so widespread will only cause fresh developers to make the same mistake, learning from the old.

**Woah!**

The use of `var` isn't bad code!

An application that was written long ago using "old money" is not bad code, the style/choices are indeed dated, doesn't make it bad code!

A lot of developers starting in JS (who hasn't sat in a class room to study) are usually using the Interweb to gain examples, the internet doesn't purge so to speak, so examples are indeed used, and built upon.

So I guess this falls under Point 1??

Point 2: I'm not sure, when I learn a different trick, I like to show it off quite a bit 🤓

Point 3: Like @hotNipi says, it works - there is no need to fix it (it's not bad code).

I, indeed like to make Sure I am not using old money, but I approach It as a tip for the user, and to update them.

Its a very critical topic - many many many views here 😅

---

<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: [9 October 2024 20:16 UTC](https://discourse.nodered.org/t/js-people-still-use-var/92258/19 "2024-10-09T20:16:53Z")

</div>

> [@ThingsTinkerer](#):
>
> You don't agree that var shouldn't be used?

No, I think that is too simplistic.

"It shouldn't be used most of the time."

There are a few edge cases where having the variable hoisted is very useful and saves time when needing to get things done quickly. As others have said, time is valuable.

But in general, I agree, I always use linting and have a favourite, slightly adjusted version of "Standard" JavaScript that I prefer to use. Full Standard is rather overly aggressive for my liking.

But then, I'm not part of a team and if I were, I'd need to work to the team standards not my own.

> [@jbudd](#):
>
> I do try and use it though because people say it is in some mysterious way better

😄 It does take a while to get used to it. But in general, it does save you from some subtle bugs that can be really hard to track down. I don't regret training myself to commit `var` to the very bottom of my bag of tricks.

> [@ThingsTinkerer](#):
>
> What matters is when you start publishing your code for others to read, learn from or even maintain.

That is certainly a good point. Though one doubtless countered by many open source developers who might say that they don't develop for that reason.

Personally, I like to write the best code I can, and often refactor old code if I come across it in the future with more knowledge to hand. But that is me and one of the reasons I code personally is for the creative joy of it. So I personally consider that time investment well worth while.

But even I will only do this if there is also some other reason to update things. I don't have the capacity to rewrite old code just for the sake of it I'm afraid.

> [@Colin](#):
>
> You are presumably to young to have experienced the joys of Basic

🤣 Actually, BBC Basic was a superb and well structured language. 😄 But yes, it was certainly easy to trip yourself up. Just be thankful that you never had to deal with APL! I once had to spend an entire week pondering over a calculated goto in APL.

> [@marcus-j-davies](#):
>
> The use of `var` isn't bad code!

Hmm, I'd say that _mostly_ the use of `var` in _new_ code is bad code. It does greatly annoy me when people trying to show others how to do things in articles in 2024 use `var` indiscriminately. There is no call for that. What's worse is that this then gets picked up in LLM's and so you end up with perpetuated bad code.

The use of `var` in old code is expected. But worthy of updating if making amendments anyway and if time allows. I have found bugs lurking due to incorrect use of `var`, so there is certainly value in this.

---

<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: [9 October 2024 20:30 UTC](https://discourse.nodered.org/t/js-people-still-use-var/92258/20 "2024-10-09T20:30:40Z")

</div>

> [@TotallyInformation](#):
>
> Actually, BBC Basic was a superb and well structured language.

Oh yes, I had forgotten BBC basic, how could I have done that? I was thinking about MS Basic.

> [@TotallyInformation](#):
>
> be thankful that you never had to deal with APL

I did try and understand it at one point, but decided life was too short.

[Next page](https://discourse.nodered.org/t/js-people-still-use-var/92258.md?page=2)
