Let's do a little test, don't search on Baidu or write code to test.
What is the output of the following:
1 + 1
'1' + 1
1 + true
NaN + 1
NaN + '1'
null + 1
null + '1'
[1] + 1
[1,2] + [1]
[1] + {n : 1}
If you are a bit confused about these operations, then you probably haven't understood implicit type conversion in addition.
What are the rules for addition?
A picture will make it clear.
Ok, let's look at an example.
Looking at the picture above, let's summarize.
Both sides are primitive types, not strings, so they need to be converted to number types.
It's equivalent to 0 + NaN, so the result is NaN.
For example, [1] + 1 from above.
It contains an object type, so valueOf is called.
After calling valueOf, it's not a primitive type, so toString is called.
It has been converted to a primitive type, following the steps above, since it contains a string, everything is converted to a string and added, so the result is '11'.
Let's look at another example.
It throws an error here, indicating that it cannot convert this object to a primitive type. This is because valueOf and toString cannot convert obj to a primitive type.
These things often appear in interview questions, testing whether you have a good understanding of the knowledge points. Let's work hard, everyone.