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 1051685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:58:28+00:00 2026-05-16T16:58:28+00:00

I need something of a HashMap in javascript. Can we do this: var map

  • 0

I need something of a HashMap in javascript. Can we do this:

var map = {};
map['foo'] = true;
map['zoo'] = true;
...

if (map['foo']) {
    // yes, foo exists.
}
else {
    // no, foo does not exist.
}

how do we properly check for the existence of the property without inserting it if it does not exist? For example, I don’t want map.foo to exist after the above check if I didn’t explicitly add it,

Thanks

  • 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-05-16T16:58:29+00:00Added an answer on May 16, 2026 at 4:58 pm

    In your example, checking for:

    if (map['foo']) {
      //..
    }
    

    Not only checks if the property is not defined on the object, the condition expression of the if statement will evaluate to false also if the property holds a value that coerces to false on boolean context (aka falsy values), such as 0, NaN, an empty string, null, undefined and of course false, e.g.:

    var map = {
      'foo': 0 // or any other falsy value
    }; 
    
    if (map['foo']) {
      // foo is truthy...
    } else {
      // foo is falsy or not defined
    }
    

    To check if a property exist on an object, regardless its value -which can be falsy, even undefined– , you can use the hasOwnProperty method, for example:

    var map = {};
    map['foo'] = 0;
    
    if (map.hasOwnProperty('foo')) {
      // foo exist physically on the object...
    }
    

    The only problem with this method is that if someone adds a property named hasOwnProperty to an object, it wont work, for example:

    var obj = {
        hasOwnProperty: 'bar'
    };
    

    If you execute obj.hasOwnProperty('prop'), it will give you a TypeError, because the object contains a string property that shadows the method -invoking the string would cause the error-.

    A workaround to this is to call the hasOwnProperty method directly from the Object.prototype object, for example:

    if (Object.prototype.hasOwnProperty.call(obj, 'prop')) {
      //..
    }
    

    You can also use the in operator:

    if ('prop' in obj) {
      //...
    }
    

    The difference with the first method is that the in operator checks also for inherited properties, for example:

    var obj = {};
    obj.hasOwnProperty('toString'); // false
    'toString' in obj; // true, inherited from Object.prototype.toString
    

    See also:

    • Difference between undefined and not being defined in Javascript

    Edit:

    Extending my response to the @slebetman comment, about checking if (map.foo !== undefined).

    As I was commenting, there are some concerns about accessing the undefined global property and also a semantic difference between checking a property value vs. property existence.

    The undefined global property is not defined as read-only ECMAScript 3rd Edition Standard, (is now writable = false on ES5 🙂

    In almost all implementations its value can be replaced.

    If someone makes:

    window.undefined = 'foo';
    
    // It will break your code:
    var obj = {};
    if (obj.foo !== undefined) {
      alert("This shouldn't ever happen!");
    }
    

    Also the semantic difference: by testing if map.foo !== undefined we are not technically only checking if the property exist on the object or not, a property can exist, holding undefined as a value, for example:

    var map = {
      'foo': undefined
    };
    
    map.hasOwnProperty('foo'); // true, because the property exists although
                               //       it holds the undefined value
    map.foo !== undefined;     // false
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need something like this http://jonraasch.com/blog/a-simple-jquery-slideshow but w/o the absolute positioning. Is it possible?
Ok, I need something like this: datediff(second, date_one, date_two) < 1 dates are stored
How can I create a barcode image in Java? I need something that will
let say I have this code Map<String, String> list = new HashMap<String, String>(); list.put(number1,
In addition to this quite old post , I need something that will use
What I need is something like public class Catalog { private Map<Class<?>, Object> options
I need something to get the hard link count from a file in a
I need something simple like date , but in seconds since 1970 instead of
I need something like make i.e. dependencies + executing shell commands where failing command
I need something simple; I have page where a user clicks an author to

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.