1

I was trying to build myself a small function to simpifly logging to the console in JavaScript. But for some reason it doesn't log the properties of the object, only the type of the logged object ([Object object].

<!-- language: lang-js -->

var randomObject = {

    fistname:"peter"
};

function log(message,color){

    var color = color || "green";

    console.log("%c" + message, "color:" + color + ";font-weight:bold; font-family:'Helvetica Neue'");

}

log(randomObject);

i'm grateful for any help/explanation

1 Answer 1

2

You are converting an object to a string by doing concatenation,

"color:" + color .....

When you attempt to do so, the primitive value of the object will be returned and concatenated with the target string.

If you want to print the object in a string format then use JSON.stringify(color).

console.log("%c" + message, "color:" + JSON.stringify(color) + ";font-weight:bold; font-family:'Helvetica Neue'");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.