How to check whether an Object in JavaScript has a Property?

How to check whether an Object in JavaScript has a Property?

Sample Code:
const object1 = { ‘test1’ : ‘testing’};
console.log( object1.hasOwnProperty( ‘test1’ ) );
console.log( object1.hasOwnProperty( ‘test2’ ) );
 
Output:
true

false 



Leave a Reply