How to console.log an object definition and a text in same string?

Ciprian Gheorghite picture Ciprian Gheorghite · May 10, 2015 · Viewed 21.9k times · Source

I have this JavaScript code:

console.log(obj);// [query: "wordOfTheDay"]
console.log(note + " : " + obj ); // obj does not show up

I want to make "obj" display in the same string as "note" no matter the type it come in as.

For example:

console.log("text sample : " + obj ); // text sample : [query: "wordOfTheDay"]

Thank you!

Answer

DACrosby picture DACrosby · May 10, 2015

console.log accepts any number of parameters, so just send each piece as its own param. That way you keep the formatting of the object in the console, and its all on one entry.

var obj = {
    query:  'wordOfTheDay',
    title:  'Frog',
    url:    '/img/picture.jpg'
};

console.log( "Text Here", obj);

// Text Here Object {query: "wordOfTheDay", title: "Frog", url: "/img/picture.jpg"}