Js Log Object Why Is Showing [Object Object]

JS log object why is showing [object Object]

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

Why does this log [object Object], [object Object]?

This is because there is type coercion in your expression. Try to output this: console.log('Contacts: ${JSON.stringify(this.state.contacts)}'); so your object wont be called by ToString but rather JSON.stringify will work first.

How can I display a JavaScript object?

If you want to print the object for debugging purposes, use the code:

var obj = {
prop1: 'prop1Value',
prop2: 'prop2Value',
child: {
childProp1: 'childProp1Value',
},
}
console.log(obj)

will display:

screenshot console chrome

Note: you must only log the object. For example, this won't work:

console.log('My object : ' + obj)

Note ': You can also use a comma in the log method, then the first line of the output will be the string and after that, the object will be rendered:

console.log('My object: ', obj);

Why does an object log `[object Object]` when called with `console.log`?

In both browser and nodejs environment, running console.log(appetizer) produces:

{
"name": 3,
"price": 3
}

[object Object] is only produced by running console.log(meal)

Appetizer: [object Object]
Main: [object Object]
Dessert: [object Object]
Total Price: 6

And that's normal because the objects are implicitly cast to string. These are equivalent:

const obj = { a: 1 }console.log('Hello: ' + obj)console.log('Hello: ' + obj.toString())console.log(`Hello: ${obj}`)

Getting an [object Object] instead of actual data in console.log

change it to console.log('payload', action.payload.user);

Array of objects being displayed as [object Object] and not as strings in console

Since you are concatening string, the object's toString method is called, which returns '[Object object]'. You could replace your + with a coma , to evaluate each variable in your console.log individually. :

let object = {    title: 'test1',    count: 10}
// concatenationconsole.log('object : ' + object);
// individual evaluation.console.log('object : ', object);

Why js variable show [object object] but console.log() works?

This had me thinking for a while, turns out there is a window.name global variable defined in browsers (MDN:Window.name).

According to MDN:
window.name will convert all values to their string representations by
using the toString method.

When toString is applied to an object, the output will be [object object].

So either enclose your piece of code within its own scope (probably inside a function) or rename your name variable.



Related Topics



Leave a reply



Submit