46

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!

1
  • 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); Commented May 9, 2015 at 22:57

6 Answers 6

86

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"}

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

6 Comments

Yes, this is the proper use of console.log
:D It worked! So I should pass then as different parameters. Thank you!
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.
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.
@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.
|
4

you can use

console.log(note, obj);

3 Comments

The former part of this answer is exactly what should be done. Personally, I'd delete the latter.
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. ..
I'm referring to the part where you modify the data.
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()))

Comments

2

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

Comments

1

this should work:

console.log(note, " : ", obj );

Comments

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 });

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.