Can anyone explain to me how the results of the following was evaluated?
{} + {} // NaN [] + {} // "[object Object]" {} + [] // 0 [] + [] // ""
To sum a property in an array of objects:Call the reduce() method to iterate over the array. On each iteration increment the sum with the specific value. The result will contain the sum of the values for the specific property.
There are 3 popular methods which can be used to insert or add an object to an array. The push() method is used to add one or multiple elements to the end of an array. It returns the new length of the array formed. An object can be inserted by passing the object as a parameter to this method.
Here is a full explanation of this, check it.
And note {} + {}
be NaN
if you execute it directly in the console because {}
is thought of a block rather than an object.
({}+{})
should be '[object Object][object Object]'
The real result is:
console.log({}+{}) // '[object Object][object Object]' console.log([]+{}) // '[object Object]' console.log({}+[]) // '[object Object]' console.log([]+[]) // ''
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With