Correct way to test for the existence of an object w/JavaScript

What is the best way to test for the existence of an object w/JavaScript ?

We are using Mirth msg engine to move message, Mirth uses Rhino JavaScript to evaluate scripts and return values.
but we don't know the best way to evaluate the existence of an object.

Thanks


var myObj;

if (myObj) { // code }

if (myObj == 'undefined') { // code }

if (myObj.length() > 0 )  { // code }

Open in new window

sidwelleAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
i would prefer below:

if (myObj) { // code }

Open in new window


or

if (typeof myObj !== "undefined") { // code }

Open in new window


For:
if (myObj.length() > 0 )

Open in new window

it will not worked if myObj is undefined.

For:
if (myObj == 'undefined')

Open in new window

is comparing value, and i think it's out of this scope
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sidwelleAuthor Commented:
So from what I understand, an 'if' statement will evaluate to true if:

   1.   The object exists in any way ?
   2.   A numeric expression evaluates to anything other than 0 ?
   3.   A binary expression evaluates to 'true' ?

Any other conditions that are not listed ?
Thanks
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
the "if" statement will let you code runs based on the condition specified, the condition will be evaluated whether that condition is true or not.

Any other conditions that are not listed ?
i think:
if (myObj) { // code }

Open in new window

or:
if (typeof myObj !== "undefined") { // code }

Open in new window

should be enough to evaluate the existence of an object

below is some tests for verification:
<script>
    var myObj;
    try {
        if (myObj) {
            output("if (myObj) { // code } is working fine - result: true");
        } else {
            output("if (myObj) { // code } is working fine - result: false");
        }
    } catch (e) {
        output("error evaluating condition: if (myObj) { // code }");
    }

    try {
        if (typeof myObj !== "undefined") {
            output("if (typeof myObj !== \"undefined\") { // code } is working fine - result: true");
        } else {
            output("if (typeof myObj !== \"undefined\") { // code } is working fine - result: false");
        }
    } catch (e) {
        output("error evaluating condition: if (typeof myObj !== \"undefined\") { // code }");
    }

    try {
        if (myObj == 'undefined') {
            output("if (myObj == 'undefined') { // code } is working fine - result: true");
        } else {
            output("if (myObj == 'undefined') { // code } is working fine - result: false");
        }
    } catch (e) {
        output("error evaluating condition: if (myObj == 'undefined') { // code }");
    }

    try {
        if (myObj.length() > 0) {
            output("if (myObj.length() > 0) { // code } is working fine - result: true");
        } else {
            output("if (myObj.length() > 0) { // code } is working fine - result: false");
        }
    } catch (e) {
        output("error evaluating condition: if (myObj.length() > 0) { // code }");
    }

    function output(v) {
        console.log(v);
    }
</script>

Open in new window


you should get a result like this:
if (myObj) { // code } is working fine - result: false
if (typeof myObj !== "undefined") { // code } is working fine - result: false
if (myObj == 'undefined') { // code } is working fine - result: false
error evaluating condition: if (myObj.length() > 0) { // code }

Open in new window

0
sidwelleAuthor Commented:
Thanks for the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.