What is the difference between object key and object key in JavaScript?

Objects in JavaScript are non-primitive data types that hold an unordered collection of key-value pairs.

What is the difference between object key and object key in JavaScript?

As you can see in the image above, the key is the property, and each object value must have a key.

When interacting with objects, situations might arise that require you to check if a particular key exists. It is important to note that if you know a key exists that automatically means that a value exists. This value could be anything – even empty, null, or undefined.

In this article, we will learn the various methods to check if an object's key exists in JavaScript.

Here's an Interactive Scrim about How to Check if an Object Has a Key in JavaScript

In case you are in a rush, here are the two standard methods we can use to check:

// Using in operator
'key' in object

// Using hasOwnProperty() method
object.hasOwnProperty('key')

How to Check if an Object Has a 'key' in object 2 in JavaScript with the 'key' in object 3 Operator

You can use the JavaScript

'key' in object
3 operator to check if a specified property/key exists in an object. It has a straightforward syntax and returns
'key' in object
5 if the specified property/key exists in the specified object or its prototype chain.

The syntax when using the

'key' in object
3 operator is:

'key' in object

Suppose we have an object which contains a user's details:

let user = {
  name: "John Doe",
  age: 40
};

We can check if a key exists with the

'key' in object
3 operator as seen below:

'name' in user; // Returns true
'hobby' in user; // Returns false
'age' in user; // Returns true

Note: The value before the

'key' in object
3 keyword should be of type
'key' in object
9 or
let user = {
  name: "John Doe",
  age: 40
};
0.

How to Check if an Object Has a 'key' in object 2 in JavaScript with the let user = { name: "John Doe", age: 40 }; 2 Method

You can use the JavaScript

let user = {
  name: "John Doe",
  age: 40
};
2 method to check if a specified object has the given property as its property. T

his method is pretty similar to the

'key' in object
3 operator. It takes in a
'key' in object
9 and will return
'key' in object
5 if the
'key' in object
2 exists in the object and
let user = {
  name: "John Doe",
  age: 40
};
8 otherwise.

The syntax when using the

let user = {
  name: "John Doe",
  age: 40
};
2 method is:

object.hasOwnProperty('key')

Suppose we have an object which contains a user's details:

let user = {
  name: "John Doe",
  age: 40
};

We can check if a key exists with the

'key' in object
3 operator as seen below:

user.hasOwnProperty('name'); // Returns true
user.hasOwnProperty('hobby'); // Returns false
user.hasOwnProperty('age'); // Returns true

Note: The value you pass into the

let user = {
  name: "John Doe",
  age: 40
};
2 method should be of type
'key' in object
9 or
let user = {
  name: "John Doe",
  age: 40
};
0.

Since we now know that these methods exist, we can now use a condition to check and perform whatever operation we wish to perform:

if ("name" in user) {
  console.log("the key exists on the object");
}

// Or

if (user.hasOwnProperty("name")) {
  console.log("the key exists on the object");
}

Wrapping Up

In this article, we have learned how to check if an object has a key using the two standard methods. The difference between the two methods is that

'name' in user; // Returns true
'hobby' in user; // Returns false
'age' in user; // Returns true
4 looks for a key in an object alone while the
'key' in object
3 operator looks for the key in the object and its prototype chain.

There are other methods you can use, but at some point they might get too elaborate and aren't that easy to understand. They also might fail when tested against certain conditions.

For example, we could use the optional chaining, so if a specified key does not exist, it will return

'name' in user; // Returns true
'hobby' in user; // Returns false
'age' in user; // Returns true
6:

let user = {
  name: "John Doe",
  age: 40
};

console.log(user?.name); // Returns John Doe
console.log(user?.hobby); // Returns undefined
console.log(user?.age); // Returns 40

So we could create a condition that, when it's not equal to

'name' in user; // Returns true
'hobby' in user; // Returns false
'age' in user; // Returns true
6, it means the key exists:

if (user?.hobby !== undefined) {
  console.log("The key exists on the object");
}

As we said earlier, these methods fail when tested against some uncommon conditions. For example, in a situation when a particular key is set to "undefined", as seen below, the condition fails:

'key' in object
0

Another example when it works but gets elaborate is when we use the

'name' in user; // Returns true
'hobby' in user; // Returns false
'age' in user; // Returns true
8 method alongside the
'name' in user; // Returns true
'hobby' in user; // Returns false
'age' in user; // Returns true
9 method. This works but isn't really easy to understand:

'key' in object
1

In the code above, we retired all the keys as an array and then applied the

'name' in user; // Returns true
'hobby' in user; // Returns false
'age' in user; // Returns true
9 method to test whether at least one element in the array passed the test. If it passes, it returns
'key' in object
5, else
let user = {
  name: "John Doe",
  age: 40
};
8.

Happy coding!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT


What is the difference between object key and object key in JavaScript?
Joel Olawanle

Frontend Developer & Technical Writer


If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

What is an object key JavaScript?

Object.keys() The Object.keys() static method returns an array of a given object's own enumerable string-keyed property names.

What is the difference between object keys and object values in JavaScript?

The keys of an object is the list of property names. The values of an object is the list of property values. The entries of an object is the list of pairs of property names and corresponding values.

What is the difference between object keys and object getOwnPropertyNames?

getOwnPropertyNames(a) returns all own properties of the object a . Object. keys(a) returns all enumerable own properties. It means that if you define your object properties without making some of them enumerable: false these two methods will give you the same result.

Can an object be a key in an object JavaScript?

Can you use objects as Object keys in JavaScript? # The short answer is "no". All JavaScript object keys are strings.