• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 56
  • Last Modified:

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

0
sidwelle
Asked:
sidwelle
  • 2
  • 2
1 Solution
 
Ryan ChongCommented:
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
 
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 ChongCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now