JS log object why is showing [object Object]

后端 未结 3 693
天涯浪人
天涯浪人 2020-11-29 09:50

In JS, if I log a string to the console it is not showing properly ?

console.log(uniqueProducts); //
console.log(\"uniqueProducts:\"+uniqueProducts);
         


        
相关标签:
3条回答
  • 2020-11-29 10:15

    You are concatenating an object to string

    You can console a string and an object by separating it by comma(,)

    you can console.log("uniqueProducts:", uniqueProducts );

    0 讨论(0)
  • 2020-11-29 10:21

    You are trying to concatenate an object with a string. You can fix it one of two ways:

    1. Remove + from the log call

      console.log("uniqueProducts:", uniqueProducts );

    2. You can use JSON.stringify to print the object as JSON:

      console.log("uniqueProducts:", JSON.stringify(uniqueProducts));

    0 讨论(0)
  • 2020-11-29 10:31

    + concatenates strings but object is not a string.

    Use console.dir(obj) to display the content of the object.

    0 讨论(0)
提交回复
热议问题