What is the difference between this expressions

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;

:white_check_mark: 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.

msg.payload = 0;
return msg;

:white_check_mark: 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.

msg = {payload: 0};
return msg;

:warning: 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; // :cross_mark: not valid in most cases

:white_check_mark: 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.

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

Summary Table

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

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

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.