Skip to content
Advertisement

Javascript object check if property exists

I have a javascript object called file, I am trying to check if this object has file.xhr.response property contained in it. I have tried like this..

JavaScript

This works when file.xhr.response exists but if it doesn’t then it throws an error…

JavaScript

Where am I going wrong?

Advertisement

Answer

You can check if object property exists using:

JavaScript

Code:

JavaScript

But if you are dealing with a complex/bigger object you can recursively search for the property within the object

Code:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement