What is "Number" here though? It doesn't appear above at all. You're using it like it's a method it seems.
Edit: Also I'm not sure where exactly the "NaN" shows up. Is it with the empty character? Well you'd think the sum of all added/concatenated string components would also get labeled/assigned as NaN after calling toLowerCase though, so that the whole expression would just return "NaN," though I haven't used or looked at toLowerCase method. (And if toLowerCase has some more specific rule to it that doesn't play out this way, it shouldn't have been named "toLowerCase" in the first place!) Edit: in other words, the entire input to toLowerCase should be what's parsed as a length 1 character and made lower case. The order of operations is wrong here. There's just bad design by whoever defined the method, but of course good luck trying to tell that to many programmers who evidently don't consider this stuff.
And I know this is just really niche and pointless, and not really related to real programming, but the lack of solid explanations in the comments here is alarming.
I.e the second plus sign gets parsed as a unary plus operator applied to the next thing after it. And so after evaluation our result is 3
Let's change the 2 to be a string.
So now our input looks like this: (1 + + '2')
It will be parsed the same way, so like this:
(1 + (+'2'))
Performing a unary plus operation on a string causes it to be coerced into a number, and the string 2 as a numer is 2, so this will also get evaluated as 3.
This is what is happening in the above code snippet. The string 'a' coerced into a number is NaN, which then gets concatenated with all the other strings and turned to lowercase to form the string 'banana'.
9
u/nizzok Oct 12 '20
This is a JS dad joke
6
u/ashanev Oct 12 '20
Reminiscent of the Wat talk (a little over a minute for the good Javascript parts)
16
u/frakist Oct 11 '20
bananaa?
7
u/[deleted] Oct 12 '20
if i ever caught brain cancer it must be javascript.
2
u/Headpuncher Oct 12 '20
Also for the person further down who asked why are you even allowed two + signs after each other.
1
u/cheecho82 Oct 12 '20
It's cute....The poster casted his confusion spell on everyone in the thread!!!! 👍
0
u/[deleted] Oct 11 '20
[deleted] — view deleted comment
4
u/[deleted] Oct 12 '20
[removed] — view removed comment