Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 684
Ciprian Gheorghite Avatar asked May 09 '15 22:05

Ciprian Gheorghite


People also ask

How do you display an object in console log?

Method 1 — Use console.log() method called with an object or objects as arguments will display the object or objects.

How do you display an object as a string?

Stringify a JavaScript ObjectUse the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.

Can we use console log in script tag?

We can use the console. log() method to display JavaScript values. To do that, we need to activate debugging in our browser with F12, and select "Console" in the debugger menu.


1 Answers

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"}
like image 67
DACrosby Avatar answered Sep 19 '22 15:09

DACrosby