# Context is reset to default when input is 0

**URL:** <https://discourse.nodered.org/t/context-is-reset-to-default-when-input-is-0/24914>\
**Category:** General\
**Created:** [15 April 2020 22:09 UTC](https://discourse.nodered.org/t/context-is-reset-to-default-when-input-is-0/24914 "2020-04-15T22:09:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Assi](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@Assi](https://discourse.nodered.org/u/Assi)\
**Post date:** [15 April 2020 22:09 UTC](https://discourse.nodered.org/t/context-is-reset-to-default-when-input-is-0/24914/1 "2020-04-15T22:09:06Z")

</div>

Im testing my function with a inject node of topic dim with payload values of 0 and 1.

the code is like:

**node.warn("xx context.dimpre:" + context.dim);**  
context.dim = context.dim || 100.0;  
**node.warn("xx context.dimpost:" + context.dim);**

if (msg.topic === 'dim')  
context.dim = msg.payload;  
node.warn("xx msg.topic " + msg.topic + " msg.payload:" + msg.payload);  
node.warn("xx context.dim:" + context.dim);

When I send \>0 everything is as expected, context.dim is persisting the value.  
When I send a 0 then it stores the value first:  
"xx context.dim:0"

But afterwards when the node gets triggered again from another msg the value changes to its init value:

16.4.2020, 00:05:03[node: percent in 2 colors](http://192.168.0.8:1880/?instance=0#)function : (warn)

**"xx context.dimpre:0"**

16.4.2020, 00:05:03[node: percent in 2 colors](http://192.168.0.8:1880/?instance=0#)function : (warn)

"xx context.dimpost: **100**"

Why is this happening?  
Thank you for any hint

---

<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:** [15 April 2020 22:35 UTC](https://discourse.nodered.org/t/context-is-reset-to-default-when-input-is-0/24914/2 "2020-04-15T22:35:27Z")

</div>

because of this line

```auto
context.dim = context.dim || 100.0;

```

When you set `context.dim` to `0`, the next time it runs, it gets the 100.0

THE REASON IS... `0 == false`

Do this instead...

```auto
if(context.dim == "undefined"){
  context.dim = 100.0 //init value ONLY if its undefined
}

```

NOTE: accessing context in this manner is depreciated (see the node red documentation on context.get, context.set)

---

<div class="post-metadata">

**Author:** ![Assi](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@Assi](https://discourse.nodered.org/u/Assi)\
**Post date:** [16 April 2020 11:11 UTC](https://discourse.nodered.org/t/context-is-reset-to-default-when-input-is-0/24914/3 "2020-04-16T11:11:14Z")

</div>

understood, its a short circuit evaluation. Thank you

---

<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:** [30 April 2020 11:11 UTC](https://discourse.nodered.org/t/context-is-reset-to-default-when-input-is-0/24914/4 "2020-04-30T11:11:17Z")

</div>

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