How to print object in JavaScript? [SOLVED]


Written By - Olorunfemi Akinlua
Advertisement

Everything in JavaScript is an object. So simply, in order to print objects in JavaScript, you will need to use the console.log() method. This method takes an object as input and prints it to the console. In this article, we will show the different ways to use the console.log() method with objects to print objects.

 

Method-1: Use console.log to print an Object in JavaScript

The way to log (or print data) in JavaScript is via the console.log() method. So, if you want to print an object, you can by passing it to the console.log() method. This will print the object in its entirety. For example, the obj binding links to an object with four properties, and then passed to the console.log() method, it prints all the properties within curly braces.

const obj = {
    name: "Femi",
    status: "Junior",
    age: 23,
    isActive: true,
};

console.log(obj);

Output

{ name: 'Femi', status: 'Junior', age: 23, isActive: true }

 

Method-2: Use object.propertyName notation to print Object values in JavaScript

However, if you only need specific properties within the object, you can by passing the property name to the console.log() method. So, if we want to print only the isActive property, we can via the object.propertyName dot notation.

const obj = {
    name: "Femi",
    status: "Junior",
    age: 23,
    isActive: true,
};

console.log(obj.isActive);

Output

true

You can also print multiple properties of an object by passing in an array of property names as you would with strings.

const obj = {
    name: "Femi",
    status: "Junior",
    age: 23,
    isActive: true,
};

console.log(obj.isActive, obj.age);

Output

true 23

 

Method-3: Use stringify() to print objects in JavaScript

If you want to print an object in a more readable format, you can use the JSON.stringify() method. This method takes an object as input and returns a JSON-formatted string. Using the same obj binding as an example, the below code illustrates the behavior of the JSON.stringify() method.

Advertisement
console.log(JSON.stringify(obj));

Output

{"name":"Femi","status":"Junior","age":23,"isActive":true}

The above output is a string with a JSON format.

 

Method-4: Use for loop to print object values in JavaScript

We can print out all the object values by looping through the object. The for...in loop will be effective in looping through the object, and obtaining the values to be logged.

const obj = {
    name: "Femi",
    status: "Junior",
    age: 23,
    isActive: true,
};

for (const value in obj) {
    console.log(value);
}

Output

name
status
age
isActive

 

Method-5: Use Object.values() method to print object values in JavaScript

If we need the values within the object as an array, we can make use of the instance method called Object.values(). This method takes an object and returns an array of the object’s property values. Let’s illustrate this by passing the obj binding to the method.

const obj = {
    name: "Femi",
    status: "Junior",
    age: 23,
    isActive: true,
};

console.log(Object.values(obj));

Output

[ 'Femi', 'Junior', 23, true ]

 

Summary

The only way to print (or log) an object is via the popular console.log() method. There are different approaches such as printing the object in its entirety, printing only the object properties, printing the object as a JSON-formatted string, or printing the object’s property values as an array.

 

References

JSON.stringify() - JavaScript | MDN (mozilla.org)
console.log() - Web APIs | MDN (mozilla.org)
Object.values() - JavaScript | MDN (mozilla.org)

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment