Programming Language

In Javascript, How Would You Print Out The Contents Of An Object?

Printing out the contents of an object in Javascript is quite beneficial when debugging your Javascript code. Following are some ways for printing out an object.

For/in loop of The Javascript

You can print out the contents of a Javascript object with the use of for/in loop. This loop is totally different when compared to a regular for loop. This is because the for/in loop is particularly used for iterating through the contents of an object. Below is an illustration of for/in loop’s general structure.

for( variable in object)


While using the for/in loop, the actual object should be the string that comes after the “in”, thus whatever your object name is, you can place it there. And any arbitrary name can be given to the string that comes before the “in” operator.

Console.log to print out Javascript objects

In case, you do not want to use a Javascript alert because you think that alert boxes are irritating, then Console.log is another option that you can use for this procedure. It is a clean and simple way to print out the Javascript object’s contents, and it does not need you to iterate over each and every property like you have to do for the for/in loop. However, you must work with a console.

Now, a question comes in mind that what is a console? Well, you can think of it as the Javascript debugger. Whether an add-on or built-in, a console comes in with every major browser.

The most famous debugging tool is the Firebug, whereas IE, Chrome and Safari all have their own developer tools that will also display a Javascript console.

Printing out Javascript objects with JSON.stringify function

Another easy way of printing out the contents of a Javascript object is the utilization of the JSON.stringify function. JavaScript Object Notation (JSON) is actually a standard that is particularly used for data interchange. It is an amazing way of printing things out because there is no need to write out a for/in loop and you don’t have to use the console just for getting properties to print out. This is the most preferred method of printing out Javascript objects.


Leave a Reply