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 if js property exists in class

myObj.hasOwnProperty(myProp)
Comment

check property exists

let person = {
   firstName: 'John',
   lastName: 'Doe'
};

let result = 'firstName' in person; 
console.log(result); // true

result = 'age' in person;
console.log(result); // false
Code language: JavaScript (javascript)
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 property exists

let result = person.hasOwnProperty('firstName');
console.log(result); // true
Code language: JavaScript (javascript)
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

check property exists

let person = {
   firstName: 'John',
   lastName: 'Doe',
   age: undefined
};

let result = person.age !== undefined;
console.log(result); // false
Code language: JavaScript (javascript)
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery get all checkbox checked 
Javascript :: isarray 
Javascript :: require express 
Javascript :: javascript create array from 1 to n 
Javascript :: org.json.JSONException: End of input at character 0 of 
Javascript :: json rename key 
Javascript :: react native new line character 
Javascript :: node uuid 
Javascript :: react js materilize 
Javascript :: jquery get mouse page left and top 
Javascript :: js make node with string 
Javascript :: react native get mac address 
Javascript :: javascript tofixed 
Javascript :: javascript:void 
Javascript :: javascript newline in alert box 
Javascript :: new jsonobject java 
Javascript :: merge two objects javascript 
Javascript :: how to loop through array of numbers in javascript 
Javascript :: cypress click link contains text 
Javascript :: javascript onclick image 
Javascript :: replace spaces with backslash js 
Javascript :: how could you implement javascript into java 
Javascript :: javascript hard reload 
Javascript :: js add element to front of array 
Javascript :: hex to rgba in js 
Javascript :: REACt helmet og tags DONT WORK 
Javascript :: javascript format rupiah 
Javascript :: jquery detect change in textarea content 
Javascript :: how to print hello world using js 
Javascript :: Return certain fields with populate from mongoose 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =