Determine If an Object Has a Property in JavaScript

Description

The following code shows how to determine If an Object Has a Property.

Example


<!DOCTYPE HTML>
<html>
<body>
<script type="text/javascript">
var myData = {<!--from  w  ww  .j  a v  a  2 s . co m-->
name : "JavaScript",
weather : "Good",
};
var hasName = "name" in myData;
var hasDate = "date" in myData;
var hasWeather = "weather" in myData;
document.writeln("HasName: " + hasName);
document.writeln("HasDate: " + hasDate);
document.writeln("HasWeahter: " + hasWeather);
</script>
</body>
</html>

Click to view the demo

The code above generates the following result.

Determine If an Object Has a Property in JavaScript