# Let or const with use of if statement

**URL:** <https://discourse.nodered.org/t/let-or-const-with-use-of-if-statement/81675>\
**Category:** General\
**Tags:** function-node\
**Created:** [29 September 2023 18:28 UTC](https://discourse.nodered.org/t/let-or-const-with-use-of-if-statement/81675 "2023-09-29T18:28:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mick/32/80990_2.png) [@Mick](https://discourse.nodered.org/u/Mick)\
**Post date:** [29 September 2023 18:28 UTC](https://discourse.nodered.org/t/let-or-const-with-use-of-if-statement/81675/1 "2023-09-29T18:28:41Z")

</div>

Which is the better method of writing JavaScript functions? 1st one uses let variable and 2nd uses a const variable.

```auto
let isRadioOn;
 if (global.get("radios").length !== 0) {
    isRadioOn = 1
 }else  
   isRadioOn = 0;

```

or

```auto
const isRadioOn = function() { 
   if (global.get("radios").length !== 0) {
    return 1;
 }else  
   return 0;
 }

```

Thanks in advance...

---

<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:** [29 September 2023 18:40 UTC](https://discourse.nodered.org/t/let-or-const-with-use-of-if-statement/81675/2 "2023-09-29T18:40:55Z")

</div>

Honestly, if your not going to alter the value of `isRadioOn` after it's been evaluated,  
I would go with the function method - I don't think there is much of a difference

BONUS - this is my preference so is neither right or wrong, but I tend to use arrow functions, especially if dealing with outer context

```auto
const isRadioOn = () => {
 if (global.get("radios").length !== 0) {
    return 1;
 } else {
    return 0;
}

```

However!  
You might need to consider if using the value multiple times in your code block - therefore another run off the function methods, might yield an updated output

---

<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:** [29 September 2023 19:05 UTC](https://discourse.nodered.org/t/let-or-const-with-use-of-if-statement/81675/3 "2023-09-29T19:05:01Z")

</div>

What about something like:

```auto
const isRadioOn = global.get("radios").length !== 0

```

Executed only once in the function node which is the more common use-case and returns `true` or `false`.

If you really want a numeric return:

```auto
const isRadioOn = global.get("radios").length !== 0 ? 1 : 0;

```

---

<div class="post-metadata">

**Author:** ![Mick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mick/32/80990_2.png) [@Mick](https://discourse.nodered.org/u/Mick)\
**Post date:** [29 September 2023 20:12 UTC](https://discourse.nodered.org/t/let-or-const-with-use-of-if-statement/81675/4 "2023-09-29T20:12:46Z")

</div>

I like it. I just learned about ternary operators but the trick is know when to use it... Appreciate the schooling..

I also wanted to add that I was familiar with

```auto
const isRadioOn = global.get("radios").length !== 0

```

and it produces a Boolean value. However the course I am taking the instructor advised they never used. That is why I did not use it.

Regards

---

<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:** [29 September 2023 20:43 UTC](https://discourse.nodered.org/t/let-or-const-with-use-of-if-statement/81675/5 "2023-09-29T20:43:01Z")

</div>

I generally don't use it if the ternary version is longer than a standard if 🙂

There is an occasional exception though. When wanting to use a const assignment, you can't do that inside an `if` block if you want the const outside the block. So if you don't want to use `let` instead, then a ternary is still useful.

---

<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:** [13 October 2023 20:43 UTC](https://discourse.nodered.org/t/let-or-const-with-use-of-if-statement/81675/6 "2023-10-13T20:43:08Z")

</div>

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