# Explanation of code help

**URL:** https://discourse.nodered.org/t/explanation-of-code-help/76319
**Category:** General
**Tags:** function-node
**Created:** [8 March 2023 19:54 UTC](https://discourse.nodered.org/t/explanation-of-code-help/76319 "2023-03-08T19:54:37Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)
#### Post date: [8 March 2023 19:54 UTC](https://discourse.nodered.org/t/explanation-of-code-help/76319/1 "2023-03-08T19:54:38Z")

</div>

@cymplecy posted this code back in January

```auto
if (msg.payload == 1) {
    context.set("previousState",!context.get("previousState"))
    msg.payload = 1 * context.get("previousState");
    return msg;
}

```

I have used this a number of times now and spent the better part of a day googling as to how it worked. I figure if I'm going to use a lot I should understand it. Unfortunately, my google searches all point back to the forum so I am left with no explanation. If someone could please tell me what is happening I would appreciate it.

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [8 March 2023 20:23 UTC](https://discourse.nodered.org/t/explanation-of-code-help/76319/2 "2023-03-08T20:23:30Z")

</div>

So what happens is the following, if msg.payload == 1.

in the first line:

```auto
 context.set("previousState",!context.get("previousState"))

```

First you get previousState variable from the node context. The content of the is negated by ! - so you have instead of true, false or vice versa. So it acts like a toggle switch. The opposite value is then immediately stored again in the node-context.

The next line:

```auto
msg.payload = 1 * context.get("previousState");

```

As boolean true evaluates to 1 and false to 0 the result is 0 or 1.

To be honest I think the code is in my opinion a little bit laborious.

In my opinion it would be sufficient to use only booleans.

---

<div class="post-metadata">

### Author: ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)
#### Post date: [8 March 2023 20:28 UTC](https://discourse.nodered.org/t/explanation-of-code-help/76319/3 "2023-03-08T20:28:11Z")

</div>

Thanks for the help

---

<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: [22 March 2023 20:28 UTC](https://discourse.nodered.org/t/explanation-of-code-help/76319/4 "2023-03-22T20:28:29Z")

</div>

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