-1

(Using javascript)

I've seen some answers saying dictionary.has("key") will evaluate to true or false based on if the key is in the dictionary. For some reason, this hasn't worked for me at all. Only dictionary["key"] != "undefined" works for me. Why is this?

  • 1
    There is no such thing as `dictionary` in JavaScript – CertainPerformance May 29 '21 at 15:10
  • https://pietschsoft.com/post/2015/09/05/javascript-basics-how-to-create-a-dictionary-with-keyvalue-pairs – that_javascript_girl May 29 '21 at 15:14
  • 2
    Perhaps you're thinking of a [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) - by the way, the link you supplied never uses `.has` ... so ... not sure what you actually read to think an Object has `has` – Jaromanda X May 29 '21 at 15:14
  • `dictionary["key"] != "undefined"` checks if `dictionary.key` is not the string `"undefined"`. Not sure why you would compare against the string. Which resource states that a `has` method exists? Which prototype does it supposedly exist on? See [How much research effort is expected of Stack Overflow users?](//meta.stackoverflow.com/q/261592/4642212) and read the docs of [`hasOwnProperty`](//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty) and [Maps’ `has`](//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has). – Sebastian Simon May 29 '21 at 15:35

1 Answers1

1

I think you mean object in JS

To evaluate if a key exists in an object:

if ('key' in dictionary)

Otherwise, to evaluate if the key does not exist

if (!('key' in dictionary))

PD: has is used for maps https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has

Ref: How do I check if an object has a key in JavaScript?