# Change node enhancement idea: Add JavaScript function as a change option

**URL:** <https://discourse.nodered.org/t/change-node-enhancement-idea-add-javascript-function-as-a-change-option/67796>\
**Category:** Feature Requests\
**Created:** [16 September 2022 10:33 UTC](https://discourse.nodered.org/t/change-node-enhancement-idea-add-javascript-function-as-a-change-option/67796 "2022-09-16T10:33:31Z")\
**Posts on this page:** 5\
**Page:** 1

<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:** [16 September 2022 10:33 UTC](https://discourse.nodered.org/t/change-node-enhancement-idea-add-javascript-function-as-a-change-option/67796/1 "2022-09-16T10:33:31Z")

</div>

Fresh back from holiday and looking at the change node something dropped into place in my mind about a slight frustration with it.

We now have lots of nice options for the change node but I think that an obvious one may be missing.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/d/0d6760bc7e7f6b66a4db557c2ea686418bdff5b4.png)

Why can't we have a simple **_JavaScript Function_** option that opens a JavaScript editor? The idea being to end the function with `return xxxxx` that would be the output for the change.

Of course, we would also need some variables being made available such as `msg`, `context`, etc, the same as for the JSONata option.

For many uses where you need to do something dynamic, currently, the only options are to resort to a function node or try to grapple with JSONata in a change node. The suggested change would enable people to make simple dynamic changes without either of those.

Thoughts?

---

<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:** [16 September 2022 10:54 UTC](https://discourse.nodered.org/t/change-node-enhancement-idea-add-javascript-function-as-a-change-option/67796/2 "2022-09-16T10:54:26Z")

</div>

The problem is you end up with two different places to put JavaScript code, and the requirements/capabilities are very different between the two.

For example, we would not allow calling `node.send()` from the Change node. Nor would we want the code to cause other side effects, such as calling `flow/global.set()`. You wouldn't be able to import other modules etc.

My concern is that leads to lots of confused users who are given some example code to try, then try using it in the wrong place and it not working because it was meant to be used in the other place.

---

<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:** [16 September 2022 11:40 UTC](https://discourse.nodered.org/t/change-node-enhancement-idea-add-javascript-function-as-a-change-option/67796/3 "2022-09-16T11:40:32Z")

</div>

Good points of course. Perhaps the most important being the one about side-effects. Not providing the `RED` or `node` objects would help but blocking the context/flow/global .set functions might be more complex.

I think though (without having tested) that node.js's vm enclosure could be used to provide sufficient isolation to prevent side-effects?

As for confusion between a function node and a JS change option. I get that it could be somewhat of a thing. But I'm not sure that it would be a big issue? A different UI in the change node would help I think so that there are clear visual differences.

I do believe that this would be a valuable addition to the change node and would certainly reduce the number of function nodes in use and in many cases, avoid the complex forum discussions that can happen when we advise people without much JS experience to use a function nodes just for a simple msg variable adjustment.

Perhaps some experimentation needed to see whether the idea is viable.

---

<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:** [16 September 2022 11:50 UTC](https://discourse.nodered.org/t/change-node-enhancement-idea-add-javascript-function-as-a-change-option/67796/4 "2022-09-16T11:50:10Z")

</div>

> [@TotallyInformation](#):
>
> in many cases, avoid the complex forum discussions that can happen when we advise people without much JS experience to use a function nodes just for a simple msg variable adjustment.

Personally I don't see the complexity being "Use a Function node with this code...". The code itself is where the complexity would be, and being able to put that code in the Change node doesn't really affect that.

In fact, I think it risks making those discussions more complex as people share JS snippets in the forum and user's don't necessarily know if it was intended for a function node or change node.

---

<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:** [15 November 2022 11:50 UTC](https://discourse.nodered.org/t/change-node-enhancement-idea-add-javascript-function-as-a-change-option/67796/5 "2022-11-15T11:50:23Z")

</div>

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