Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between '[Object]' and '[object Object]' in Chrome console?

Tags:

javascript

I have some code like the followings.

MyRequests.cors_request("POST", 
        APP_CONFIG.APP_URL+"/users/selectAllUsers", 
        null, 
        function ok(users) {  

            $scope.usersNotFiltered = users;

            console.log('users--->', users);
            console.log('$scope.userPerSystem--->', $scope.userPerSystem);

            // delete the items that is already exists in the userPerSystem
            function filterUsers(element, index, array) {

                console.log("$scope.usersNotFiltered :: " + users);
                commonFindAndRemove($scope.usersNotFiltered, 'userId', element.userId);
                console.log('a[' + index + '] = ' + element.userId);
            }

            $scope.userPerSystem.forEach(filterUsers);

            $scope.users = $scope.usersNotFiltered;   
}, 

And I run tihs code and watch the console. What is the difference between '[Object]' and '[object Object]' in chrome console?

users---> [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]

$scope.userPerSystem---> [Object]0: Objectlength: 1__proto__: Array[0] redca-ias.concat.js:5258

$scope.usersNotFiltered :: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

like image 999
verystrongjoe Avatar asked May 04 '15 07:05

verystrongjoe


1 Answers

The first log shows the structure as it is, i.e. the live representation of the structure and the second one is the string representation of the structure.

> var arr = [{}, {}, {}];
> arr // simply logs the object as it is
> [Object, Object, Object]
> arr + '' // converts the structure into string
> "[object Object],[object Object],[object Object]"

You get this result as you are concatenating a string with an object:

"$scope.usersNotFiltered :: " + users

In the above snippet JavaScript converts the structure into a string (a primitive value) and string representation of an object is [object Object]. Since the structure is an array of objects, string representation of each element is joined with ,. You get similar result by calling Array.prototype.toString() or Array.prototype.join() method:

> var arr = [1, 2, 3];
> arr.toString()
> "1,2,3"
> var arrayOfObjects = [{}, {}, {}];
> arrayOfObjects.toString()
> "[object Object],[object Object],[object Object]"
like image 149
undefined Avatar answered Oct 10 '22 00:10

undefined