node js function return [object Object] instead of a string value

I also encountered this issue, running the following code in a node.js terminal in conjunction with "watchman-make" (watchman-make: see comments in first answer at https://www.quora.com/What-IDEs-are-available-for-node-js-development-on-Linux).

The following code (with node.js output shown) illustrates the points made in the accepted answer/comments:

function arrayToList(array) {
  var list = {};
  for (var i = array.length - 1; i >= 0; i--) {
    list = {value: array[i], rest: list};
  }
  return list;
};

console.log(arrayToList( [1, 2, 3, 4, 5] ));
// { value: 1,
//  rest: { value: 2, rest: { value: 3, rest: [Object] } } }

// '[Object]' ?
// http://stackoverflow.com/questions/34264800/node-js-function-return-object-object-instead-of-a-string-value

var obj = arrayToList([1, 2, 3, 4, 5]);

console.log('%j', obj);
// {"value":1,"rest":{"value":2,"rest":{"value":3,"rest":{"value":4,"rest":{"value":5,"rest":null}}}}}

console.log(JSON.stringify(obj));
// {"value":1,"rest":{"value":2,"rest":{"value":3,"rest":{"value":4,"rest":{"value":5,"rest":null}}}}}

[object Object] occurs in the log when there is an object with keys and values. You can access properties in an object wth dot notation (.) e.g

objectName.propertyName

If properyName is another object it will still return [object Object] and so you need to look for another property within that. Properties could also contain methods (functions). If you want to get the string version of an object in order to compare them for example, then use

JSON.stringify(objectName);

When using console.log with node and you have a deeply nested object, you may not be able to view the nested object contents. In that case you can use:

console.log(util.inspect(objectName, false, null));

To view the entirety of the object. Although you must require util in the file.


Maybe you have something like:

const myObject = { hello: 'world' };
console.log('My object: '+myObject);

The problem with this is that it converts myObject to a string in the console e.g. using myObject.toString(). In this case, you can make it easier for yourself and separate it like this:

const myObject = { hello: 'world' };
console.log('My object:', myObject);

And the console can now interpret myObject and display it nicely.


Use one of the options:

console.log("%o", yourObjName)
console.log(JSON.stringify(yourObjName))
console.log(JSON.stringify(yourObjName, null, 3))