# Function node code editor warnings

**URL:** https://discourse.nodered.org/t/function-node-code-editor-warnings/14748
**Category:** General
**Created:** [24 August 2019 20:08 UTC](https://discourse.nodered.org/t/function-node-code-editor-warnings/14748 "2019-08-24T20:08:02Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![thatcadguy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thatcadguy/32/5273_2.png) [@thatcadguy](https://discourse.nodered.org/u/thatcadguy)
#### Post date: [24 August 2019 20:08 UTC](https://discourse.nodered.org/t/function-node-code-editor-warnings/14748/1 "2019-08-24T20:08:02Z")

</div>

Are these warnings/errors displayed by the line numbers part of Node-RED's codebase, or is this functionality from another library?

The warnings/errors shown are wrong sometimes, e.g. defining a variable with var inside an if statement and then using it right after says it's out of scope (that would be true for let or const, not var).

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [24 August 2019 20:11 UTC](https://discourse.nodered.org/t/function-node-code-editor-warnings/14748/2 "2019-08-24T20:11:47Z")

</div>

It is the JavaScript parser that the ACE editor comes with.

I know it trips over some of the newer ES6 syntax.

Can you provide some specific examples?

---

<div class="post-metadata">

### Author: ![thatcadguy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thatcadguy/32/5273_2.png) [@thatcadguy](https://discourse.nodered.org/u/thatcadguy)
#### Post date: [24 August 2019 20:19 UTC](https://discourse.nodered.org/t/function-node-code-editor-warnings/14748/3 "2019-08-24T20:19:35Z")

</div>

This is within a function definition:

```auto
  // create request data
  if ([3, 4, 5, 6].includes(fc)) {
    var n = 2;
    var reqd = Buffer.from([(val >> 8) & 0xff, val & 0xff]);
  } else if (fc === 16) {
    const n_val = val.length;
    if (n_val > 127)
      throw 'Number of data values exceeds limit (127)';
    var n = 2*n_val + 3;
    var reqd = Buffer.concat([Buffer.from([0, n_val, 2*n_val]), ...val.map(x => Buffer.from([(x >> 8) & 0xff, x & 0xff]))]);
  } else
    throw 'Function code not implemented';

  // fill in message length
  n += 4;
  reqh[4] = (n >> 8) & 0xff;
  reqh[5] = n & 0xff;

  return Buffer.concat([reqh, reqd]);

```

It complains that n is already defined with at the 2nd "var n =" statement inside the else if (not true), complains @ fill in message length that n, reqh, and reqd are out of scope (not true, var is function scoped)

I've also used "== null" to test if something is null or undefined, and it gripes about how I need to use === to test against null. IMO it should say "be careful of the difference between == and ===" or something like that.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [24 August 2019 20:23 UTC](https://discourse.nodered.org/t/function-node-code-editor-warnings/14748/4 "2019-08-24T20:23:12Z")

</div>

`var` is function scoped and you have used `var n` twice in this function.... so the error message is correct.

---

<div class="post-metadata">

### Author: ![thatcadguy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thatcadguy/32/5273_2.png) [@thatcadguy](https://discourse.nodered.org/u/thatcadguy)
#### Post date: [24 August 2019 20:36 UTC](https://discourse.nodered.org/t/function-node-code-editor-warnings/14748/5 "2019-08-24T20:36:31Z")

</div>

The `var n`'s are in different branches of an if statement, only one will be executed.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [24 August 2019 20:48 UTC](https://discourse.nodered.org/t/function-node-code-editor-warnings/14748/6 "2019-08-24T20:48:17Z")

</div>

Doesn't matter. They are in the same function so will both get hoisted to the start of the function.

---

<div class="post-metadata">

### Author: ![thatcadguy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thatcadguy/32/5273_2.png) [@thatcadguy](https://discourse.nodered.org/u/thatcadguy)
#### Post date: [24 August 2019 21:01 UTC](https://discourse.nodered.org/t/function-node-code-editor-warnings/14748/7 "2019-08-24T21:01:11Z")

</div>

Touché, it appears you are correct. Oh goody, yet another JS quirk...I assumed that it worked like C/C++.

---

<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: [24 August 2019 21:03 UTC](https://discourse.nodered.org/t/function-node-code-editor-warnings/14748/8 "2019-08-24T21:03:47Z")

</div>

Never assume haha.

I mostly use `let` or `const` in block scope to avoid the side affects.
