xxxxxxxxxx
const user1 = {
username: "john"
};
const user2 = {
username: "duo"
authority: "grepper"
}
// check object property
console.log(user1.hasOwnProperty('authority')); // false
console.log(user2.hasOwnProperty('authority')); // true
xxxxxxxxxx
const hero = {
name: 'Batman'
};
hero.hasOwnProperty('name'); // => true
hero.hasOwnProperty('realName'); // => false
xxxxxxxxxx
var person = {'first_name': 'bill','age':20};
if ( person.hasOwnProperty('first_name') ) {
//person has a first_name property
}
xxxxxxxxxx
const userOne = {
name: 'Chris Bongers',
email: 'info@daily-dev-tips.com',
};
const userTwo = {
name: 'John Do',
};
console.log(userOne.hasOwnProperty('email'));
// Returns: true
console.log(userTwo.hasOwnProperty('email'));
// Returns: false
xxxxxxxxxx
if (typeof receviedData?.d?.heartbeat_interval != "undefined") {
}
// OR
if (receviedData?.d?.heartbeat_interval !== undefined) {
}
xxxxxxxxxx
// the coolest way
const obj = {
weather: 'Sunny;
}
if('weather' in obj) {
// do something
}
xxxxxxxxxx
const employee = {
id: 1,
name: "Jhon",
salary: 5000
};
const isSalaryExist = "salary" in employee;
console.log(isSalaryExist); //true
const isGenderExist = "gender" in employee;
console.log(isGenderExist); //false