Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7854935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:14:21+00:00 2026-06-02T20:14:21+00:00

I have a simple function in my library that checks the validity of an

  • 0

I have a simple function in my library that checks the validity of an object reference (object here meaning, a reference to a created HTML element, mostly DIV’s). It looks like this:

function varIsValidRef(aRef) {
    return ( !(aRef == null || aRef == undefined) && typeof(aRef) == "object");
}

While experimenting i found that this has the same effect:

function varIsValidRef(aRef) {
    return (aRef) && typeof(aRef) == "object";
}

I understand there are some controversies regarding the short hand () test? While testing against various datatypes (null, undefined, integer, float, string, array) i could find no difference in the final outcome. The function seem to work as expected.

Is it safe to say that these two versions does the exact same thing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:14:23+00:00Added an answer on June 2, 2026 at 8:14 pm

    No, in my opinion these functions don’t work the same:

    First option
    If aRef is not undefined or null and the type of the var is object it returns true.

    Second option
    First we convert aRef to a boolean. Values like null, undefined and 0 become false, everything else become true. If it is true (so not one of those values) it checks if the type is object.

    So the second option return false if aRef is 0, something you don’t want. And it isn’t option a elegant way to check it, because you check if an object or string or something is equal to a boolean.

    And at least they don’t return the same thing. The first option returns a boolean, but the second option returns the value you put in the function if (aRef) is false:

    varIsValidRef(0);
    >>> 0
    
    varIsValidRef('');
    >>> ""
    
    varIsValidRef(undefined);
    >>> undefined
    
    varIsValidref(null);
    >>> null
    

    Because JavaScript use these values as falsy values you don’t see the difference between these return values if you use if statements or something like that.

    So I suggest you to use the first option.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple class library that I use in Excel. Here is a
I have a simple function which disables/enables select element when checkbox is checked/unchecked. It
I have a simple function that does nothing: function doNothing(){} I also have a
I have a simple function that returns an NSString after decoding it. I use
I have this simple function: <script type=text/javascript> //<![CDATA[ jQuery(function($){ function here(b){alert(b);} ; here(6); });
I have a simple function that shows loading spinner while fetching data (usually takes
I have a function in an external library that I cannot change with the
i am writing a simple function for a library, that will take in as
It should be simple, but it's not. I have a library that is hoping
I have simple example: function File(name) { this.name = name this.text = null }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.