SoFunction
Updated on 2025-04-10

Comparison of two methods for determining whether an object has a certain key

1. Two ways to judge

Method 1:"key" in obj, the result is false, indicating that it is not included; otherwise, it means that it is included

Method 2:("key"), obj represents the object, the result is false represents not included; otherwise, it represents inclusion

2. The difference between the two methods

Both methods can be used to check if an object contains specified properties, but they have some differences.

  • “key” in obj:

    • This method is usedinOperators to check whether the attribute exists in the object.
    • If the objectobjOr its prototype chain contains an attribute named "key", then returntrue
    • If the objectobjThe property named "key" does not contain, and the property does not exist on its prototype chain, then returnfalse
  • (“key”):

    • This method is usedhasOwnPropertyMethod to check whether the object directly has the specified attribute.
    • If the objectobj Directly owned by “key” Properties of,Then return true
    • If the objectobjIf you do not directly have an attribute named "key", or if the attribute exists on its prototype chain, then returnfalse

The differences are as follows:

  • "key" in objCheck whether the attribute exists in the objectobjor in its prototype chain, not just the object itself.
  • ("key")Check whether the attribute exists directly in the objectobj, its prototype chain is not included.

3. Give an example to illustrate:

var obj = { key: "value" };
("key" in obj);                  // Output: true(("key"));     // Output: true
("toString" in obj);             // Output: true (toString is the prototype method of Object)(("toString"));// Output: false

In this example,objThe object directly has a property named "key", so both methods returntrue. At the same time, becausetoStringThe method exists inObjectThe prototype of  , so useinOperator check"toString" in objWill returntrue,but("toString")returnfalse, because this attribute does not belong directly toobjThe object's.

Therefore, the choice of using the method depends on your specific need for the existence of the attribute, whether you want to check the attributes on the object and its prototype chain, or just check the attributes of the object itself.

Attachment: JS determines whether the key in an array or object exists

Arrays and objects in JS are equivalent. To determine whether a key exists in an array (or whether the object contains a certain property), we may immediately think of using ary[key] == undefined to determine whether the key exists in this array or object, but this judgment is problematic because there may beary = {key:undefined};

The correct way should be:

(key); or (key);

In addition, when looping arrays or objects, you should use:

for(var key in ary) { (key+" : "+ary[key]); }

Summarize

This is the article about comparing the two methods of JS judging whether an object has a certain key. For more related JS judging that an object has a certain key, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!