Computer >> Computer tutorials >  >> Programming >> Javascript

How to print content of JavaScript object?


To print the content of a JavaScript object, try to run the following code. The code prints the object, with its properties and values −

Example

<!DOCTYPE html>
<html>
   <body>
      <script>
         function display(obj) {
            var res = '';
            for (var a in obj) {
               res += a + ': ' + obj[a] + '\n';
            }
            alert(res);
         }

         var newObject = {'Amit': 70778, 'Sachin': 87547, 'Saurav': 57535};
         display(newObject);
      </script>
   </body>
</html>