How to check for undefined?

I’m shifting elements out of an array. When the array becomes empty, shift returns undefined. The narrat array features don’t have a length/count function. And the documentation doesn’t say how to check for a value being undefined. I experimented with the run_js function, since Javascript can easily test “=== undefined” but I probably wasn’t integrating it into the narrat code correctly. Specifically, I was trying to put the Javascript test for undefined into a narrat “if” statement.

For example:

… code …
… there is a valid array being created …
… code ran successfully on elements that got shifted out of the array and now it’s empty …
var target (shift $array)
… I want to have code operate on $target but I need to know that its value is now undefined …

Thanks for any help!!!

Ok, I at least found a way to avoid having narrat crash:

var count (object_keys $array)
if (== $count 0)

If there’s one element left in the array then the only key you get back is 0. But this is only testing that there’s ONE element left in the array, not that it is actually empty. So you can remove this element, if necessary, and continue from this point assuming that shift/pop/etc. will fail (or at least return undefined which is problematic), but it seems far from optimal.

If there’s a better solution, please let me know, thanks!!!

Hello Aaron,

You can check for undefined in an conditional statement like found below.

if (== $variable undefined):
  // do something or not