# Throw error when javascript variable is undefined

**URL:** <https://forum.juce.com/t/throw-error-when-javascript-variable-is-undefined/35157>\
**Category:** General JUCE discussion\
**Created:** [September 10, 2019, 1:02am UTC](https://forum.juce.com/t/throw-error-when-javascript-variable-is-undefined/35157 "2019-09-10T01:02:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elan\_Hickler](https://avatars.discourse-cdn.com/v4/letter/e/8baadc/32.png) [@Elan\_Hickler](https://forum.juce.com/u/Elan_Hickler)\
**Post date:** [September 10, 2019, 1:02am UTC](https://forum.juce.com/t/throw-error-when-javascript-variable-is-undefined/35157/1 "2019-09-10T01:02:42Z")

</div>

Is there an easy way to ensure that variables are all defined before running the script? Like… cancel the script if any variable is undefined, throw an error or something.

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [September 10, 2019, 5:34pm UTC](https://forum.juce.com/t/throw-error-when-javascript-variable-is-undefined/35157/2 "2019-09-10T17:34:01Z")

</div>

The script code needs to run before any errors can be caught. (The Juce JavaScriptEngine is not a compiler, the code is interpreted and executed as it runs.) If a variable was left undefined, you can check for that in your C++ code by using var::isUndefined after the script has run. Also JavaScriptEngine::execute returns a Result, which will tell you if there was an error parsing or executing the script code.

---

<div class="post-metadata">

**Author:** ![Elan\_Hickler](https://avatars.discourse-cdn.com/v4/letter/e/8baadc/32.png) [@Elan\_Hickler](https://forum.juce.com/u/Elan_Hickler)\
**Post date:** [September 10, 2019, 6:07pm UTC](https://forum.juce.com/t/throw-error-when-javascript-variable-is-undefined/35157/3 "2019-09-10T18:07:57Z")

</div>

It’s just that I have hundreds of “setMethod” functions and not sure how to go about checking undefined in all of them. I’d have to write that if statement over and over. Maybe there’s a better way.

---

<div class="post-metadata">

**Author:** ![Elan\_Hickler](https://avatars.discourse-cdn.com/v4/letter/e/8baadc/32.png) [@Elan\_Hickler](https://forum.juce.com/u/Elan_Hickler)\
**Post date:** [September 16, 2019, 10:07pm UTC](https://forum.juce.com/t/throw-error-when-javascript-variable-is-undefined/35157/4 "2019-09-16T22:07:57Z")

</div>

I might have to modify the juce javascript code. Is there one place in the code that evaluates variables that I can throw in a check for undefined?

---

<div class="post-metadata">

**Author:** ![Elan\_Hickler](https://avatars.discourse-cdn.com/v4/letter/e/8baadc/32.png) [@Elan\_Hickler](https://forum.juce.com/u/Elan_Hickler)\
**Post date:** [September 17, 2019, 2:16am UTC](https://forum.juce.com/t/throw-error-when-javascript-variable-is-undefined/35157/5 "2019-09-17T02:16:58Z")

</div>

How do I make this throw an error, where in the javascript code could I put a check?

```auto
newVariable = variable + undefined; // and other operators
newVariable += undefined; // and other operators

```

I figured out how to throw an undefined error for a few other situations by adding a check in Javascript::RootObject::Assignment:

```cpp
    struct Assignment : public Expression
    {
        Assignment (const CodeLocation& l, ExpPtr& dest, ExpPtr& source) noexcept : Expression (l), target (dest), newValue (source) {}

        var getResult (const Scope& s) const override
        {
            var value (newValue->getResult (s));

			if (value.isUndefined())
				location.throwError("Assigning an undefined variable.");

            target->assign (s, value);
            return value;
        }

        ExpPtr target, newValue;
    };

```
