# Variables without var, let, const?

**URL:** <https://discourse.nodered.org/t/variables-without-var-let-const/96284>\
**Category:** General\
**Created:** [27 March 2025 08:54 UTC](https://discourse.nodered.org/t/variables-without-var-let-const/96284 "2025-03-27T08:54:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![JeromeVISUALS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeromevisuals/32/40103_2.png) [@JeromeVISUALS](https://discourse.nodered.org/u/JeromeVISUALS)\
**Post date:** [27 March 2025 08:54 UTC](https://discourse.nodered.org/t/variables-without-var-let-const/96284/1 "2025-03-27T08:54:49Z")

</div>

Hi,

I found some Js code examples and want to use and adapt them in NR function nodes.

I discovered that in Js sometimes variables can be declared without var, let and so on, and then are declared as global variables.

From what I can see NR does not support that in function nodes, we need to specify for all variables ?

Thanks in advance

---

<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:** [27 March 2025 09:07 UTC](https://discourse.nodered.org/t/variables-without-var-let-const/96284/2 "2025-03-27T09:07:20Z")

</div>

It is supported but strongly discouraged. If you really want to write bad code (my opinion), let me know and I'll point you to the settings that will permit that.

---

<div class="post-metadata">

**Author:** ![JeromeVISUALS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeromevisuals/32/40103_2.png) [@JeromeVISUALS](https://discourse.nodered.org/u/JeromeVISUALS)\
**Post date:** [27 March 2025 09:09 UTC](https://discourse.nodered.org/t/variables-without-var-let-const/96284/3 "2025-03-27T09:09:01Z")

</div>

Ha ha no problem ! I prefer staying with good practices, and will adapt the code, will be much better.

Thanks for your answer !

---

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [27 March 2025 09:13 UTC](https://discourse.nodered.org/t/variables-without-var-let-const/96284/4 "2025-03-27T09:13:24Z")

</div>

why would you want to do this? If you want to retain a variable outside of the function node scope, you can use context variables.  
In any case, relying on external variables (as opposed to message properties) can introduce race conditions between multiple concurrent flows (or runs of the same flow).

---

<div class="post-metadata">

**Author:** ![JeromeVISUALS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeromevisuals/32/40103_2.png) [@JeromeVISUALS](https://discourse.nodered.org/u/JeromeVISUALS)\
**Post date:** [27 March 2025 09:15 UTC](https://discourse.nodered.org/t/variables-without-var-let-const/96284/5 "2025-03-27T09:15:01Z")

</div>

> [@omrid](#):
>
> ou can use context variables.  
> In any case, relying on exter

Hi, actually I don't, just wanted to understand the behavior, why it works in some Js examples but not in NR.  
Bus as I said I'll tweak the code to use it the proper way.

---

<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:** [27 March 2025 10:54 UTC](https://discourse.nodered.org/t/variables-without-var-let-const/96284/6 "2025-03-27T10:54:24Z")

</div>

> [@JeromeVISUALS](#):
>
> we need to specify for all variables ?

As others have said, it is best to use best practices from the outset. Use of `const` where you don't need to later replace the variable's value, otherwise use of `let`. Both of these constrain the context of the resulting variable to help avoid hard to track down bugs in your code.

In extreme situations, you might still choose to use `var` - I sometimes get lazy in a function node and if I want to specify a variable but wrap it in a try/catch, and I might use a var there simply for speed. But it is still bad practice.

---

<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:** [31 March 2025 19:51 UTC](https://discourse.nodered.org/t/variables-without-var-let-const/96284/7 "2025-03-31T19:51:47Z")

</div>

I always thought that, if not specified, then js defaulted to `var` rather than something different. Is an unspecified type actually functionally different to `var` other than that it produces warnings in the syntax checker?

---

<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:** [31 March 2025 20:10 UTC](https://discourse.nodered.org/t/variables-without-var-let-const/96284/8 "2025-03-31T20:10:02Z")

</div>

If you don't declare a variable at all, this is what happens:

- The variable automatically becomes **global** (even if inside a function).
- It is **not hoisted** , so it only exists after assignment.
- It implicitly becomes a **property of the global object**.

Whereas declaring with `var`:

- The variable is scoped to the **function** if defined in one. It becomes a **property of the global object** (in browsers, `window`) if defined outside a function.
- It is automatically **hoisted** to the top of its scope but initialized as `undefined`. So you can use it BEFORE you've defined 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:** [31 March 2025 20:15 UTC](https://discourse.nodered.org/t/variables-without-var-let-const/96284/9 "2025-03-31T20:15:42Z")

</div>

Thanks for the clarification.

That just goes to show why one should always use `let` or `const` rather than `var` or nothing.

---

<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:** [14 April 2025 20:16 UTC](https://discourse.nodered.org/t/variables-without-var-let-const/96284/10 "2025-04-14T20:16:39Z")

</div>

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