Anyone explain me this?


“2,asde,[object Object][object Object]”

You can’t add a list and an object.

ok bro, can you explain further what is happening by adding a array(containing object) with object

It’ll throw an error, because

Maybe you want .push({ … }) (or .concat([{ … }])) to add an object to the array?

@M-Michelini As you can see, it was added.

@osumami It is because of type conversions. You can go through this article to understand what is happening behind the scenes:

In your example, you are adding array and object. It is different types of data, so JS will covert it to one type and add them. It takes the first argument of your sum function(array) and converts it to a string, then it converts the object to a string([object Object] is a string representation of the object) and then sums 2 strings.


bro just explain me why javascript handle addition of array and object as string, also why object are shown in square brackets

My mistake, tried copying this into my terminal but the apostrophe’s don’t copy and paste properly from the forum :S

1 Like

The addition operator only works with Strings and Numbers, so when you try to add something that isn’t a string or a number, it will convert it to a string so it can perform the operation.

The default conversion from an object to string is "[object Object]".

1 Like

JavaScript attempts to do what you’re trying to do even though it doesn’t make sense. The only way it can complete the operation is if it converts the objects to strings then joins those strings together. Note an array is a type of object as well.

Objects all have a method called toString which is used to do the conversion. Different objects have different results when the method is called:

  • arrays print the values, with commas in-between each one.
  • standard objects just print a message saying they are an object.

The capitalised first word you can read as being the specific type of object, the second is the general type, so [Object object] is an object of type Object (ie it has no special defined type, it’s just the one you get out-of-the-box).