How to print content of JavaScript object?

JavascriptObject Oriented ProgrammingFront End Technology

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>
raja
Published on 27-Feb-2018 10:38:20
Advertisements