Enumerating an Object's Properties - Javascript Language Basics

Javascript examples for Language Basics:Introduction

Introduction

You enumerate the properties an object has using the for...in statement.

Demo Code

ResultView the demo in separate window

      <!DOCTYPE HTML>
      <html>
          <head>
              <title>Example</title>
          </head>
          <body>
              <script type="text/javascript">
                  var myData = {//from  w  w  w. j  a va  2 s.  c o m
                      name: "java2s.com",
                      topic: "CSS",
                      printMessages: function() {
                          document.writeln("Hello " + this.name + ". ");
                          document.writeln("Today is " + this.topic + ".");
                      }
            };

            for (var prop in myData) {
                document.writeln("Name: " + prop + " Value: " + myData[prop]);
            }

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

Related Tutorials