38
var object = { name: 'Harry', age: '25', sex: 'male'...... n}; 

This object has 'n' number of properties which I don't know and I would like to print these whole properties.

3

2 Answers 2

56

There are heaps of solutions from a quick Google, a recomended result is; Print content of JavaScript object?

console.log(JSON.stringify(object, null, 4)); 

The second argument alters the contents of the string before returning it. The third argument specifies how many spaces to use as white space for readability.

Sign up to request clarification or add additional context in comments.

1 Comment

This fails for objects with cycles in their membership.
30

You can use the The Object.keys() function to get an array of the properties of the object:

var obj = { name: 'Harry', age: '25', sex: 'male'}; Object.keys(obj).forEach((prop)=> console.log(prop));

1 Comment

that's too many console.log's; better collect that into a string.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.