Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to check wether the property exist in a object in java script

if (obj.hasOwnProperty('prop')) {
    // do something
}
Comment

test if property exists javascript

const hero = {
  name: 'Batman'
};

hero.hasOwnProperty('name');     // => true
hero.hasOwnProperty('realName'); // => false
Comment

Check object property exists or not in js

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
Comment

JavaScript check if property exists in Object

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
Comment

check if property exists javascript

// the coolest way
const obj = {
 weather: 'Sunny; 
}

if('weather' in obj) {
 // do something 
}

Comment

check property exists in object javascript

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
Comment

PREVIOUS NEXT
Code Example
Javascript :: check if value is boolean 
Javascript :: Navigator.pushReplacementNamed parameters 
Javascript :: upload files to express using express-fileupload 
Javascript :: javascript next month from date 
Javascript :: uploading file with fetch in js 
Javascript :: simple kick command discord.js v12 
Javascript :: shadow react native 
Javascript :: eintegrity npm error 
Javascript :: ajax file form 
Javascript :: moment js get date 1 month 
Javascript :: mongoose join multiple collections 
Javascript :: set timeout JS for loop 
Javascript :: recursion javascript 
Javascript :: chalk js 
Javascript :: maths 
Javascript :: generate random number in node js 
Javascript :: is javascript good 
Javascript :: react native header 
Javascript :: how to set env variables in js 
Javascript :: js list of objects 
Javascript :: get current time in different timezone javascript 
Javascript :: modal.hide not working 
Javascript :: resize function in addEventListener JS 
Javascript :: find highest value in array javascript 
Javascript :: javascript splice without changing array 
Javascript :: js regex replace multiple matches 
Javascript :: upload image react 
Javascript :: move canvas element to mouse 
Javascript :: preview upload image js 
Javascript :: formdata array of objects 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =