# What is the difference between this expressions

**URL:** <https://discourse.nodered.org/t/what-is-the-difference-between-this-expressions/97738>\
**Category:** General\
**Created:** [22 June 2025 13:39 UTC](https://discourse.nodered.org/t/what-is-the-difference-between-this-expressions/97738 "2025-06-22T13:39:03Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Janvi](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@Janvi](https://discourse.nodered.org/u/Janvi)\
**Post date:** [22 June 2025 13:39 UTC](https://discourse.nodered.org/t/what-is-the-difference-between-this-expressions/97738/1 "2025-06-22T13:39:03Z")

</div>

Sorry - I am new with JS. What is the difference between

msg.payload = 0;  
return msg;

and

msg = {payload:0];  
return msg;

and

return 0;

and maybe to discontinue the flow

return null;

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [22 June 2025 13:47 UTC](https://discourse.nodered.org/t/what-is-the-difference-between-this-expressions/97738/2 "2025-06-22T13:47:23Z")

</div>

✅ 1.` msg.payload = 0; return msg;`

What it does:

```
Modifies the existing msg object (a standard Node-RED message).

Sets its payload property to 0.

Sends the updated message to the next node.

```

Use case: Most common way to manipulate a message and continue the flow.

```auto
msg.payload = 0;
return msg;

```

✅ 2.

> msg = {payload: 0}; return msg;

What it does:

```
Creates a new object with only a payload property set to 0.

Replaces the entire message with this new one.

Sends the new message to the next node.

```

Risk: You lose all other fields in the original msg (like topic, timestamp, etc.), unless you copy them manually.

```auto
msg = {payload: 0};
return msg;

```

⚠ 3. `return 0;`

What it does:

```
Returns the number 0, not a message object.

Node-RED expects a message object, so this will be ignored or cause an error depending on the node.

```

Result: Does not continue the flow correctly. Don’t use this unless you know the node can handle raw values (rare).

`return 0; //` ❌ not valid in most cases

✅ 4. return null;

What it does:

```
Explicitly tells Node-RED to not send anything forward.

Effectively halts the flow from this function node.

```

Use case: Conditional filtering. Only continue flow when certain conditions are met.

```auto
if (msg.payload > 50) {
    return msg;
} else {
    return null; // Stops the flow
}

```

### Summary Table

| Code | Message Sent? | Notes |
| --- | --- | --- |
| `msg.payload = 0; return msg;` | ✅ Yes | Keeps the rest of the `msg` intact. |
| `msg = {payload: 0}; return msg;` | ✅ Yes | Replaces entire message. |
| `return 0;` | ❌ No | Not valid; return value must be a message object. |
| `return null;` | ❌ No | Stops the message from flowing further. |

---

<div class="post-metadata">

**Author:** ![Janvi](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@Janvi](https://discourse.nodered.org/u/Janvi)\
**Post date:** [23 June 2025 11:57 UTC](https://discourse.nodered.org/t/what-is-the-difference-between-this-expressions/97738/3 "2025-06-23T11:57:25Z")

</div>

Great table and explanations. If we like to pass more than one value to next node, we need to open a second dimension in the JSON object what seems normally the "topic" for NodeRed. Its also possible to use the topic with only one value.

`msg = {payload:21}; // generates new JSON object with content 21`

seems equal to

`msg = {"payload":{"temperature":21}}; // generates new JSON object with content 21`

The benefit for the topic temperature is now, to add a second value to the same msg.payload object separated by comma

```auto
msg = {"payload":
             {"temperature":21,
              "color":"green"}
           }

```

If last expression is followed by the one before, the topic color and its value green are lost as the new object overwrites the old. If we like to change color while keeping the temperature, we have to write

`msg.payload = {"color":"blue"};`

what is equal to

`msg.payload.color = "blue";`

If we like to add a new temperature topic to existing JSON msg what already contains topic "color" its

`msg.payload = {"temperature":19};`

Changing both values in case of existing JSON object is then

`msg.payload = {"temperature":19 , "color":"blue"};`

but is not possible to write if one of the topics is not existing.  
Position of the topic:value pairs inside a JSON object seems a dont care.

`msg.payload = { "color":"blue" , "temperature":19};`

Hopefully this is all true what I understood and I do not have to amend too much. I certainly have practice and deal with to solve anything using NodeRed.

---

<div class="post-metadata">

**Author:** ![Janvi](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@Janvi](https://discourse.nodered.org/u/Janvi)\
**Post date:** [23 June 2025 13:06 UTC](https://discourse.nodered.org/t/what-is-the-difference-between-this-expressions/97738/4 "2025-06-23T13:06:09Z")

</div>

I am struggling with the use of topics. Why is the debug output format different and what is the difference at all? "green" ist not a JSON object while {"color":"green"} should be one.

```auto
[{"id":"2b88b27bd370a8f9","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"918531f828bf9a92","type":"inject","z":"2b88b27bd370a8f9","name":"ColorSensor","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"color","payload":"green","payloadType":"str","x":1270,"y":80,"wires":[["ab73115700b1d330"]]},{"id":"8f5b1d142230d240","type":"inject","z":"2b88b27bd370a8f9","name":"MeassureTemp","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":1280,"y":200,"wires":[["5b92be1fb26001dc"]]},{"id":"5b92be1fb26001dc","type":"function","z":"2b88b27bd370a8f9","name":"TempSensor","func":"msg = {\"payload\":\n {\"temperature\":21}\n };\n\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1470,"y":200,"wires":[["36784930c7326b4a"]]},{"id":"ab73115700b1d330","type":"debug","z":"2b88b27bd370a8f9","name":"debug 7","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1640,"y":80,"wires":[]},{"id":"36784930c7326b4a","type":"debug","z":"2b88b27bd370a8f9","name":"debug 8","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1640,"y":200,"wires":[]}]

```

 ![Screenshot from 2025-06-23 15-04-28](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/4713d2cb58c4dd84447fb7fbd6eab505670a0eae.png)

---

<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:** [23 June 2025 13:10 UTC](https://discourse.nodered.org/t/what-is-the-difference-between-this-expressions/97738/5 "2025-06-23T13:10:28Z")

</div>

> [@Janvi](#):
>
> If we like to pass more than one value to next node, we need to open a second dimension in the JSON object what seems normally the "topic" for NodeRed.

Your use of "topic" is confusing.

In Node-red a message will very often have two **properties** : `payload` and `topic`  
A property consists of a **key** and a **value** pair `"key": value`

Here the keys are payload and topic. The value for payload is itself an object with two key: value pairs.

```auto
{
"payload": {
   "author": "Beatrix Potter",
   "title": "The Tale of Peter Rabbit"
}"
"topic": "First Editions"
}

```

> [@Janvi](#):
>
> but is not possible to write if one of the topics is not existing.

IF msg.payload is already an object then you can add or modify a property

msg.payload.title = "The Tale of Mr Jeremy Fisher"  
msg.payload.price = 2750

---

<div class="post-metadata">

**Author:** ![Janvi](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@Janvi](https://discourse.nodered.org/u/Janvi)\
**Post date:** [23 June 2025 13:15 UTC](https://discourse.nodered.org/t/what-is-the-difference-between-this-expressions/97738/6 "2025-06-23T13:15:16Z")

</div>

Accepted. Probably my use of topic is not only confusing. My definition is simply wrong and not understood. What the hell is the output of the ColorSensor button? How do we have to change the TempSensor function code to get same format in debug window?

---

<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:** [23 June 2025 13:25 UTC](https://discourse.nodered.org/t/what-is-the-difference-between-this-expressions/97738/7 "2025-06-23T13:25:55Z")

</div>

> [@Janvi](#):
>
> Accepted, but what ... is the output of the ColorSensor button?

In the picture you posted above there is an inject node called ColorSensor.  
The double quotes in the debug panel shows that it's output is a string `green`  
The inject node is probably configured like this

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/8/2879bc874632bba8afad9d2daa5f594278d755cc.png)

> [@Janvi](#):
>
> How do we have to change the TempSensor function code to get same format in debug window?

The same format - a number?  
msg.payload = 21  
Or a string?  
msg.payload = "21"

---

<div class="post-metadata">

**Author:** ![Janvi](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@Janvi](https://discourse.nodered.org/u/Janvi)\
**Post date:** [23 June 2025 13:28 UTC](https://discourse.nodered.org/t/what-is-the-difference-between-this-expressions/97738/8 "2025-06-23T13:28:53Z")

</div>

Inject node looks like this. temperature 21 is a number. Flow is attached before.

 ![Screenshot from 2025-06-23 15-28-21](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/2/f22b17415a745403f1979c4716a25ea2dda0bf7b.png)

---

<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:** [23 June 2025 13:38 UTC](https://discourse.nodered.org/t/what-is-the-difference-between-this-expressions/97738/9 "2025-06-23T13:38:19Z")

</div>

Your picture shows that the payload output from the function is not 21 but an object `{"temperature": 21}`

You might find the documentation [Working with Messages](https://nodered.org/docs/user-guide/messages) helpful.

---

<div class="post-metadata">

**Author:** ![Janvi](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@Janvi](https://discourse.nodered.org/u/Janvi)\
**Post date:** [23 June 2025 15:40 UTC](https://discourse.nodered.org/t/what-is-the-difference-between-this-expressions/97738/10 "2025-06-23T15:40:46Z")

</div>

Finally I found the difference. It is possible to add the topic to the msg instead of the payload.  
Version 1: Adding topic to the payload gives a JSON object

```auto
msg = {"payload":
            {"temperature":21}
        };
return msg;

```

Version 2: Adding topic to the message is also possible and does not generate a JSON object

```auto
msg.payload = 21;
msg.topic = "temperature";
return msg;

```

Curly braces are main difference to produce nested JSON object.  
Who said, Node Red should be easy?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [23 June 2025 15:49 UTC](https://discourse.nodered.org/t/what-is-the-difference-between-this-expressions/97738/11 "2025-06-23T15:49:39Z")

</div>

> Who said, Node Red should be easy?

It is, except that these are concepts of javascript.

```auto
{ payload: { temperature: 21, color: "green"}} // object
{ payload:21 } // number
{ payload:"hello"} // string

```

if you write:

```auto
msg.topic = "some topic"
msg = { payload:{ temperature: 21 } }
return msg 

```

You are overwriting msg with an object.

Instead you could write:

```auto
msg.topic = "some topic"
const payload = { temperature: 21 } 
msg.payload = payload
return msg

```

Now you have both a msg.topic and a msg.payload with an object.

---

<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:** [23 June 2025 15:51 UTC](https://discourse.nodered.org/t/what-is-the-difference-between-this-expressions/97738/12 "2025-06-23T15:51:04Z")

</div>

> [@Janvi](#):
>
> Who said, Node Red should be easy?

None of what you are doing here requires you to use function nodes or write JavaScript.  
You can the change node for most of this msg property setting stuff.

---

<div class="post-metadata">

**Author:** ![Janvi](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@Janvi](https://discourse.nodered.org/u/Janvi)\
**Post date:** [23 June 2025 16:08 UTC](https://discourse.nodered.org/t/what-is-the-difference-between-this-expressions/97738/13 "2025-06-23T16:08:12Z")

</div>

Beside NodeRed I lack of JavaScript. To be honest: I understimated JavaScript completely.

Probably there is everything possible using pre-manufactured NodeRed nodes. The libraries and custom contributions have thousands of specialized nodes possibly nobody has any overview. Nobody knows what custom nodes will be maintained in future. Therefore its a good combination to add own JS code to available nodes. At least while learning. Going back to the most simple operations before using any mighty black box node without understanding. Today I struggled to use the join node what required to watch several YT videos. After I understood above differences, I hope the join node will do soon what I expect.

---

<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:** [23 June 2025 16:44 UTC](https://discourse.nodered.org/t/what-is-the-difference-between-this-expressions/97738/14 "2025-06-23T16:44:36Z")

</div>

Yeah, fair approach, however from what I have seen, you are starting off like i did (reach straight for the function node, using context, loops)

Once you get over that hump, you will find many of the core nodes (and a good set of contribs) are well thought out and reduce the need to pick the sledgehammer each time.

The join is simplest when you pass a `split` stream back into the `join` (in auto mode)

e.g. pass an array in `payload` `[1,2,3,4]` --\> `split` --\> `change` (add 10 to payload) --\> `join` (default auto mode) --\> `debug` `[11,12,13,14]`

Join node in manual mode take a moment to orient yourself but sufice to say, the defaults are _normally_ what you want (e.g. join as an key/val object, join by topic)

A good resource for learning what the nodes do is built right into node-red.

`CTRL-I` -\> examples -\> node-red

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/a/4abcf0f9f9be72a8b7bed48b25587cf423a0af0c.png)

---

<div class="post-metadata">

**Author:** ![pcb1962](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pcb1962/32/10113_2.png) [@pcb1962](https://discourse.nodered.org/u/pcb1962)\
**Post date:** [27 June 2025 10:26 UTC](https://discourse.nodered.org/t/what-is-the-difference-between-this-expressions/97738/15 "2025-06-27T10:26:23Z")

</div>

> [@dynamicdave](#):
>
> `return null;` ❌ No Stops the message from flowing further.

Is there any difference between 'return null;' and just 'return;' ?  
I've been doing the latter for years, should I change?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [27 June 2025 12:20 UTC](https://discourse.nodered.org/t/what-is-the-difference-between-this-expressions/97738/16 "2025-06-27T12:20:55Z")

</div>

No, whichever is more readable/ understandable to you.
