# Test if flow variable, i.e. context variable exists?

**URL:** <https://discourse.nodered.org/t/test-if-flow-variable-i-e-context-variable-exists/93616>\
**Category:** General\
**Created:** [2 December 2024 01:31 UTC](https://discourse.nodered.org/t/test-if-flow-variable-i-e-context-variable-exists/93616 "2024-12-02T01:31:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jibun-no-Kage](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jibun-no-kage/32/99316_2.png) [@Jibun-no-Kage](https://discourse.nodered.org/u/Jibun-no-Kage)\
**Post date:** [2 December 2024 01:31 UTC](https://discourse.nodered.org/t/test-if-flow-variable-i-e-context-variable-exists/93616/1 "2024-12-02T01:31:06Z")

</div>

Test if flow variable, i.e. context variable exists?

I have tried...

```auto
if (flow.get('Test') || false) {
node.warn('Test Exist')
} else {
node.warn('Test Not Exist')
flow.set('Test', true)
}

```

But is this legit? The examples using type and 'undefined' all seem to illustrate simple variables, not context based.

---

<div class="post-metadata">

**Author:** ![Jibun-no-Kage](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jibun-no-kage/32/99316_2.png) [@Jibun-no-Kage](https://discourse.nodered.org/u/Jibun-no-Kage)\
**Post date:** [2 December 2024 01:53 UTC](https://discourse.nodered.org/t/test-if-flow-variable-i-e-context-variable-exists/93616/2 "2024-12-02T01:53:14Z")

</div>

So typeof does handle flow variables...

```auto
if (typeof flow.get('test') == 'undefined') {
flow.set('test', false)
node.warn('not exist')
} else {
node.warn('exist')
}

```

Might be an idea to add an example of this to the Context Variable use documentation?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 December 2024 09:02 UTC](https://discourse.nodered.org/t/test-if-flow-variable-i-e-context-variable-exists/93616/3 "2024-12-02T09:02:32Z")

</div>

> [@Jibun-no-Kage](#):
>
> Might be an idea to add an example of this to the Context Variable use documentation?

This is nothing to do with context. Once you run `flow.get('test')` then you just have a javascript variable, so you can use `typeof` just the same as you would for any other variable.

What do you want to do when it is undefined? If you just want to give it a default value then you can use  
`let test = flow.get('test') ?? some_default_value`  
That will set test to the default value if it does not defined (or is null).

---

<div class="post-metadata">

**Author:** ![Jibun-no-Kage](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jibun-no-kage/32/99316_2.png) [@Jibun-no-Kage](https://discourse.nodered.org/u/Jibun-no-Kage)\
**Post date:** [2 December 2024 14:48 UTC](https://discourse.nodered.org/t/test-if-flow-variable-i-e-context-variable-exists/93616/4 "2024-12-02T14:48:55Z")

</div>

Ah "??" My lack of experience with JavaScript syntax bites me again. The lack of examples show flow variables in use versus a simple variable, implied to me that there was something special about... var A = flow.get('test') versus B = function() or such... which now seems nutty, but late last night? Well. But flow... is just what behind the curtain an object? Not a variable. Where 'get' is a JavaScript accessor I think it is called? Thanks for the tip on '??'... never seen that in Python! LOL.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 December 2024 15:25 UTC](https://discourse.nodered.org/t/test-if-flow-variable-i-e-context-variable-exists/93616/5 "2024-12-02T15:25:08Z")

</div>

The `??` operator is a fairly recent addition to javascript.

> [@Jibun-no-Kage](#):
>
> But flow... is just what behind the curtain an object? Not a variable

A variable is just a reference to a javascript object, or a javascript simple value. In the statement  
`let test = flow.get('test') ?? some_default_value`  
you are right that `flow.get("test")` is not literally a js variable, as it is not stored under an accessible name, it is the object, or value that the variable `test` will reference. However, when writing code, the syntax for using `myVariable` in an expression is exactly the same as using `flow.get('test')`.

---

<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:** [16 December 2024 15:25 UTC](https://discourse.nodered.org/t/test-if-flow-variable-i-e-context-variable-exists/93616/6 "2024-12-16T15:25:20Z")

</div>

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