An object can be used to check if it exists using 2 approaches:
Method 1: Using the typeof operator
The typeof operator returns the type of the variable on which it is called as a string. The return string for any object that does not exist is “undefined”. This can be used to check if an object exists or not, as a non-existing object will always return “undefined”.
Syntax:
if (typeof objectToBeTested != "undefined") // object exists else // object does not exist |
Example:
<!DOCTYPE html> <html> <head> <title>How to check whether an object exists in javascript</title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b>How to check whether an object exists in javascript</b> <p>Click on the button to check if the object exists</p> <p>Output for existing object: <span class="outputExist"></span></p> <p>Output for non existing object: <span class="outputNonExist"></span></p> <button onclick="checkObjectExists()"> Click here </button> <script type="text/javascript"> function checkObjectExists() { // create an existing object for comparison let existingObject = {}; if (typeof existingObject != "undefined") { ans = true; } else { ans = false } document.querySelector( '.outputExist').textContent = ans; if (typeof nonExistingObject != "undefined") { ans = true; } else { ans = false; } document.querySelector( '.outputNonExist').textContent = ans; } </script> </body> </html> |
Output:
- Before clicking the button:
- After clicking the button:
Method 2: Using a try-catch statement to catch a Reference error
Accessing a non-existing object will always throw a Reference error. A try-catch block can be used to determine this error. Any random property of the element can be accessed for this error to be thrown.
Syntax:
try { objectToBeTested.prop; // object exists } catch { // object does not exist } |
Example:
<!DOCTYPE html> <html> <head> <title>How to check whether an object exists in javascript</title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b>How to check whether an object exists in javascript</b> <p>Click on the button to check if the object exists</p> <p>Output for existing object: <span class="outputExist"></span></p> <p>Output for non existing object: <span class="outputNonExist"></span></p> <button onclick="checkObjectExists()">Click here</button> <script type="text/javascript"> function checkObjectExists() { // create an existing object for comparison let existingObject = {}; try { // accessing a random property existingObject.prop; ans = true; } catch { ans = false; } document.querySelector( '.outputExist').textContent = ans; try { // accessing a random property nonExistingObject.prop; ans = true; } catch { ans = false; } document.querySelector( '.outputNonExist').textContent = ans; } </script> </body> </html> |
Output:
- Before clicking the button:
- After clicking the button:
Recommended Posts:
- How to check a key exists in JavaScript object?
- JavaScript | Check if a key exists inside a JSON object
- How to check a JavaScript Object is a DOM Object ?
- How to access an object having spaces in the object's key using JavaScript ?
- What is the difference between Object.keys() and Object.entries() methods in JavaScript ?
- How to check whether an object is a date?
- JavaScript | Check if element exists in the visible DOM
- How to Check Mentioned File Exists or not using JavaScript/jQuery ?
- How to Check an Element with Specific ID Exists using JavaScript ?
- How to check whether the enter key is pressed in a textbox or not using JavaScript / jQuery ?
- Check whether HTML element has scrollbars using JavaScript
- How to check whether a radio button is selected with JavaScript ?
- How to check whether an array is subset of another array using JavaScript ?
- JavaScript | Check whether a URL string is absolute or relative
- How to check if an array includes an object in JavaScript?
- How to check object is an array in JavaScript?
- How to check an object is empty using JavaScript?
- PHP | Type Casting and Conversion of an Object to an Object of other class
- HTML | DOM Object Object
- How to select first object in object in AngularJS?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai


