JS

HOW TO CHECK IF PROPERTY EXISTS IN OBJECT

var obj = {   Name: "Jon",   Email: "jon@doe.com",   Gender: "Male"  };

DEMO OBJECT

01

WAYS TO CHECK (A)

02

HAS OWN PROPERTY var exist = obj.hasOwnProperty("Name"); // true

EXTRACT KEYS & CHECK var keys = Object.keys(obj); var exist = keys.includes("Email"); // true

IN OPERATOR var exist = "Gender" in obj; // true

WAYS TO CHECK (B)

03

COMPARISON OPERATOR var exist = obj["Email"] !== undefined; // true

MANUAL LOOP & CHECK var check = "Name", exist = false; for (let key in theObject) {   if (key == check) {  exist = true;  break; } } console.log(exist); // true