Open In App

How to print the content of an object in JavaScript ?

Improve
Improve
Like Article
Like
Save
Share
Report

To print the content of an object in JavaScript we will use JavaScript methods like stringify, object.values and loops to display the required data.

Let’s first create a JavaScript Object containing some key-values as given below:

Javascript




// Given Object
const obj = {
  name: 'John',
  age: 30,
  city: 'New York'
};


Examples to print the content of an object in JavaScript

1. Using JSON.stringify to print object content

JSON.stringify() is a method in JavaScript used to convert JavaScript objects into JSON strings. We can use JSON.stringify to print the content of an object in JavaScript.

Syntax:

JSON.stringify(value, replacer, space);

Example: 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; 

Explanation: This code defines an object `GFG_object` with properties and values. It also defines a function `printObj` to print the contents of an object. It recursively iterates over the object’s properties, printing each property and its value. The function handles nested objects by recursively calling itself.

2. Using console.log to print object content

To print the content of an object in JavaScript, we could use console.log

Example: Here, we are using console.log statement to print the content of an object in JavaScript.

Javascript




const myObject = { key1: 'value1', key2: 'value2', key3: 'value3' };
 
console.log(myObject);


Output

{ key1: 'value1', key2: 'value2', key3: 'value3' }

Explanation: Here, we have use console to print the content of object.

3. Using Object.values()

The Object.values() method in JavaScript returns an array of a given object’s own enumerable property values. It extracts the property values and stores them in an array.

Example: This example prints all the values in the given object.

Javascript




const obj = {
  name: 'John',
  age: 30,
  city: 'New York'
};
 
const values = Object.values(obj);
console.log(values); // Output: ['John', 30, 'New York']


Output

[ 'John', 30, 'New York' ]

Explanation: The code defines an object named `obj` with properties like `name`, `age`, and `city`. Then, it uses `Object.values(obj)` to extract the property values of `obj` and store them in an array named `values`. Finally, it prints the array `values`, which contains the values of the properties in the `obj` object.

4. Using for in loop

To print the content of an object in JavaScript, we could Loop through Object Properties.

Example: Here, we are Looping through Object Properties to print the content of an object in JavaScript.

Javascript




const myObject = { key1: 'value1', key2: 'value2', key3: 'value3' };
 
for (const key in myObject) {
    if (myObject.hasOwnProperty(key)) {
        console.log(`${key}: ${myObject[key]}`);
    }
}


Output

key1: value1
key2: value2
key3: value3

Explanation: The code iterates through each key of the `myObject`. For each key, it checks if it’s a direct property of `myObject`, then prints the key-value pair using template literals.



Last Updated : 07 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads