Friday, October 24, 2025
HomeLanguagesJavascriptHow to print the content of an object in JavaScript ?

How to print the content of an object in JavaScript ?

The JSON.stringify() method is used to print the JavaScript object. This method is used to allow to take a JavaScript object or Array and create a JSON string out of it. While developing an application using JavaScript, many times it is needed to serialize the data to strings for storing the data in a database or for sending the data to an API or web server. The data has to be in the form of the strings. This conversion of an object to a string can be easily done with the help of the JSON.stringify() function. 

Syntax:

JSON.stringify(value, replacer, space)

Example 1: This example converts the object to a string by simply traversing it and appending the object property and value to the string. 

Javascript




let GFG_object = {
    prop_1: 'val_11',
    prop_2: 'val_12',
    prop_3: 'val_13'
};
 
let printObj = function (obj) {
    let string = '';
 
    for (let prop in obj) {
        if (typeof obj[prop] == 'string') {
            string += prop + ': ' + obj[prop] + '; \n';
        }
        else {
            string += prop + ': { \n' + print(obj[prop]) + '}';
        }
    }
 
    return string;
}
 
console.log(printObj(GFG_object));


Output

prop_1: val_11; 
prop_2: val_12; 
prop_3: val_13; 

Example 2: This example using the JSON.stringify() method to convert the object to string. 

Javascript




let GFG_object = {
    prop_1: 'val_11',
    prop_2: 'val_12',
    prop_3: 'val_13'
};
   
console.log(JSON.stringify(GFG_object));


Output

{"prop_1":"val_11","prop_2":"val_12","prop_3":"val_13"}
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS