Determine If an Object Has a Property - Javascript Language Basics

Javascript examples for Language Basics:Introduction

Introduction

You can check to see if an object has a property using the in expression.

Demo Code

ResultView the demo in separate window

<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
    </head>
    <body>
        <script type="text/javascript">
            var myData = {/*from  ww  w .ja v a2  s .  c o  m*/
                name: "java2s.com",
                topic: "CSS",
            };

            var hasName = "name" in myData;
            var hasDate = "date" in myData;

            document.writeln("HasName: " + hasName);
            document.writeln("HasDate: " + hasDate);

        </script>
    </body>
</html>

Related Tutorials