Repository navigation
when to use toString() and valueOf()? #189
Answered
by
pero5ar
wjm1314
asked this question in
WTF Questions
Example(Unexpected result):var foo = { |
Answered by
pero5ar
Sep 18, 2018
Replies: 2 comments
|
The The |
0 replies
Answer selected by
denysdovhan
|
oh~~~, I got it! Thank you! |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
joinis actually perfectly expected. You're joining array elements into a string so it triggerstoStringfor each element.The
+is also expected, although a bit less, the thing is that+is primarily an arithmetic operator so it triggersvalueOf, however thevalueOfa string primitive is that string primitive and since one of the values in the expression remained a string the+falls back to being a concatenation operator. This can be seen as a bit WTF, however situations similar to that are covered in the Funny math section.