How to print the content of an object in JavaScript ?

 

<html

    <head

        <title

            Print the content of object using JavaScript

        title>

    head

      

    <body style = "text-align:center;" id = "body"

      

        <h1 style = "color:green;"

            GeeksForGeeks 

        h1>

      

        <p>

            Print JavaScript Object.

        p>

          

        <button onclick = "gfg_Run()"

            print object

        button>

          

        <p id = "GFG_DOWN" style 

               = "color:green; font-size: 20px; font-weight: bold;">

        p>

          

        <script>

            var el_down = document.getElementById("GFG_DOWN");

              

            var GFG_object = { 

                prop_1: 'val_11', 

                prop_2: 'val_12', 

                prop_3: 'val_13'

            };

              

            var printObj = function(obj) {

                var string = '';

  

                for(var prop in obj) {

                    if(typeof obj[prop] == 'string') {

                        string+= prop + ': ' + obj[prop]+'; br>';

                    }

                    else {

                        string+= prop + ': { br>' + print(obj[prop]) + '}';

                    }

                }

  

                return string;

            }

            function gfg_Run() { 

                el_down.innerHTML = printObj(GFG_object);

            }

        script

    body

html>                    

Source link

Write a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.