Deep Tech Point
first stop in your tech adventure

How to check if a property exists in a JavaScript object?

January 1, 2023 | Javascript

JavaScript is a prototypical scripting language as some others, and the object is a fundamental construct to this type of language. Lets quickly remind ourselves what is an object in JavaScript? And what about object properties? Then we can see what methods are available to check if a property exists in Javascript object.

What is JavaScript object?

JavaScript objects are simply simple data structures that you can use to quickly and concisely create a bunch of data. It’s the foundation of all programming in JavaScript. An Object in JavaScript is an unordered collection of properties.

So, our first JavaScript object looks like this: { }

In JavaScript you can create a new object by running this code:

var objectName = { someData: data, someMethod: function(){ } };

What is object property in JavaScript?

The object property represents a named value within an object. Object properties provide a way of exposing named data items to objects. Objects in JavaScript are containers for keys (properties) and values (data, aka properties-values).

The basic idea is that a JavaScript object property has two parts, the key and the value: { key : value }.

A JavaScript object can have any number of properties, and these properties can be anything, meaning any data structure.

var obj = {a: 1, b: 2, c:3};
obj.b; // output 2

How to check if a property exists in a JavaScript object?

Now lets face a real world programming scenario. We have object “obj” and we want to check if some property exists in it. It should return “true” or “false” based on the situation. What would be the easiest way to do this, just using native object.property syntax?

If obj has property “x” and obj.x doesn’t exist, we want the function to return “true”.

One of the best and powerful tool is to use JavaScript’s built-ins to check if a property exists in a JavaScript object. To do this, we first have to use if statement. If statement has a simple usage that it checks if the condition is true or false and will take actions accordingly.

if (! obj.hasOwnProperty("x")) {
  // property x does not exist
}

But we can also do this instead:

if (typeof obj.x == "undefined") {
  // property x does not exists
}

In our scenario, “just using native object.property syntax” we are using a syntactical sugar to the JavaScript standard syntax. If the result is a truthy expression, then it means that the property exists. A truthy expression is a condition evaluated to true, or a value that converts to true. A value is called “true” when it is a non-empty string, numbers, booleans, or null. A value is called “false” when it is anything other than a true value.

To check if JavaScript object property exists we can also use this (if it fits our coding style):

if(obj.property) {
  /**  
   *  do something
   */
}

Finally, the in operator is another commonly used way to do a property lookup. It checks if a property exists in an object and returns a boolean (true or false). It sounds like the most intuitive way to check if an arbitrary property exists in an object.
So we could use this:

if ("x" in obj) {
    // property x does not exist
}