43

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!

Ciprian Gheorghite
  • 535
  • 1
  • 4
  • 6
  • There's not much point to forcing all logged items into a concatenated string. `console.log` takes as many parms as you want to give it and logs them in the same log line with their types preserved. Just do: `console.log('text sample:', obj);` – JAAulde May 09 '15 at 22:57

6 Answers6

83

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"}
DACrosby
  • 11,116
  • 3
  • 39
  • 51
  • Yes, this is the proper use of `console.log` – JAAulde May 09 '15 at 23:00
  • :D It worked! So I should pass then as different parameters. Thank you! – Ciprian Gheorghite May 09 '15 at 23:01
  • Do you know why Alfonso's answer does not work? JSON.stringify(obj) returned an empty object as a string "[]". Will do some research of my own, but if you have some quick pointers, would be much appreciated. – Ciprian Gheorghite May 09 '15 at 23:06
  • Hmm, what does your object look like? If you just want to debug, then there's not much point to using `JSON.stringify`, because that just gives you a static and boring string, compared to logging it separately. –  May 09 '15 at 23:23
  • @CiprianGheorghite `JSON.stringify` should work as well, it you just want it as a string. It even works with multi-dimensional objects, though it gets harder to read than just logging the object itself. [Example Fiddle](http://jsfiddle.net/daCrosby/kas2nvzy/2/). – DACrosby May 10 '15 at 04:14
  • @PhilippeOceangermanique I've removed the broken jsFiddle link. The `console.log` does work still though; it's part of [the documentation](https://developer.mozilla.org/en-US/docs/Web/API/Console/log) still. Perhaps you're encountering a different issue? – DACrosby Jun 25 '19 at 19:28
4

you can use

console.log(note, obj);
Tech Savant
  • 3,686
  • 1
  • 19
  • 39
  • The former part of this answer is exactly what should be done. Personally, I'd delete the latter. – JAAulde May 09 '15 at 22:59
  • But doesn't console.dir give a slightly different format in the console. I left it there in case someone had a preference on how its formatted in console. Maybe I'll take it out tho. .. – Tech Savant May 09 '15 at 23:24
  • I'm referring to the part where you modify the data. – JAAulde May 09 '15 at 23:25
3

console.log can take arbitrary number of arguments so you can put all data you need to log separating it by commas.

console.log("text sample : ", obj, JSON.stringify(obj), (typeof obj), (new Date()))
shershen
  • 9,875
  • 11
  • 39
  • 60
1

this should work:

console.log(note, " : ", obj );
Julien Grégoire
  • 16,864
  • 4
  • 32
  • 57
1

Sometimes I like using string substitutions to output my obj in the middle of a string like this.

console.log('from %o to %o', obj1, obj2);

You finf all substitution here https://developer.mozilla.org/en-US/docs/Web/API/console#using_string_substitutions

Mathieu Doyon
  • 340
  • 3
  • 3
0

This is an example to add a common prefix to all console.log();

let baseLog = console.log;
console.log = function(){
    baseLog("Homepage", ...arguments);
}
console.log("hello world");
console.log(4, 5, 6);
console.log({ a: 1, b: 2 });