
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Checking if a key exists in a JavaScript object
We are required to illustrate the correct way to check whether a particular key exists in an object or not. Before moving on to the correct way let's first examine an incorrect way and see how actually its incorrect.
Way 1: Checking for undefined value (incorrect way)
Due to the volatile nature of JavaScript, we might want to check for the existence of key in an object like this −
const obj = { name: 'Rahul' };
if(!obj['fName']){}
or
if(obj['fName'] === undefined){}
These both are incorrect ways. Why?
Because in this case there happens to be no 'fName' key, but suppose there existed a 'fName' which was deliberately set to false or undefined.
Our function should have returned that the key did not exist but actually they did. So in such cases this method fails.
Way 2 − Using the in operator (Correct Way)
The in keyword just introduced in the ES6 checks for an entry in an iterable. Therefore, to check for the existence of key, we can do something like −
('fName' in obj);
Way 3 − Using hasOwnProperty() method (Correct Way)
Using the Object.prototype.hasOwnProperty() method we can determine whether or not an object contains a key.
Its syntax is −
obj.hasOwnProperty('fName');
The difference between Way 2 and Way 3 is just that Way 3 only checks for the property of the Object instance it’s called upon whereas ‘in’ checks for Object instance properties as well as inherited properties (if there are any).
- Related Articles
- Checking if a key exists in HTML5 Local Storage
- Check if a particular key exists in Java LinkedHashMap
- Check if a given key exists in Java HashMap
- How to check if a key exists in a Python dictionary?
- Checking if element exists with Python Selenium.
- How to check if a key exists in a map in Golang?
- Java Program to check if a particular key exists in TreeMap
- How Check if object value exists not add a new object to array using JavaScript ?
- How to check if a given key already exists in a Python dictionary?
- How to Check if a Key Exists in the Hashtable in C#?
- How to check if a variable exists in JavaScript?
- Javascript search for an object key in a set
- Extract key value from a nested object in JavaScript?
- Checking if a string can be made palindrome in JavaScript
- Checking if two arrays can form a sequence - JavaScript
