6
u/ashanev Oct 12 '20
Reminiscent of the Wat talk (a little over a minute for the good Javascript parts)
2
16
u/frakist Oct 11 '20
bananaa?
27
u/draq100 Oct 11 '20
part
+ + 'a' + 'a'equals to+ Number('a') + 'a'so doubleaare just to obfuscate the example5
0
u/Mr_82 Oct 12 '20 edited Oct 12 '20
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.
1
1
u/grantrules Oct 12 '20
NaN shows up because you're doing
+'a'which coerces the string 'a' to a Number which results in NaN.
7
2
u/Headpuncher Oct 12 '20
('b' + 'a' + +'115' + 'ack').toLowerCase();
"ba115ack" 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
Oct 11 '20
[deleted]
48
u/annathergirl Oct 11 '20
But why two a's after it? I'm not too savvy with JS but according to my bad logic there's one a too much.
34
u/draq100 Oct 11 '20
('b'+'a'+ +'anything'+'a').toLowerCase()works toopart
+'anything'is equivalent ofNumber('anything')and returns NaN
+ ''gives 0 as it'sNumber('')21
u/annathergirl Oct 11 '20
That makes sense. But why does it log "banana" and not "bananaa" in the screenshot?
19
37
u/draq100 Oct 12 '20 edited Oct 12 '20
okay, let's go step by step:
('b'+'a'+ + 'a'+'a').toLowerCase()becomes (is processed as)
('b'+'a'+ Number('a')+'a').toLowerCase()which becomes
('b'+'a'+ NaN +'a').toLowerCase()(NaN is still a Number here)which becomes:
('b'+'a'+ String(NaN) +'a').toLowerCase()after casting to string:
('b'+'a'+ 'NaN' +'a').toLowerCase()in the end it's
('baNaNa').toLowerCase()and finally, our result:
'banana'12
8
u/VikaashHarichandran Oct 12 '20
I just tried this out, the picture is indeed correct.
That's because the string token after two consecutive symbol tokens will be considered as NaN by the lexer.
Correct me if I'm wrong.
3
u/densch92 Oct 12 '20
careerspressadvertiseblogTermsContent policyPrivacy policyMod policyReddit Inc © 2020. All rights reserved
Amkes sense then.
also why the fuck do I see the above text shit when i simply try to reply to VikaashHarichandran, isntead of the actual commen ttext? O_o
1
1
u/bwallker Jul 02 '22
This has nothing to do with the lexer
Consider the expression (1 + + 2)
This will get parsed like this:
(1 + (+2))
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'.
4
4
u/redderper Oct 11 '20
Exactly, nothing weird about it really. People see memes like this one and go on programming subs to comment that JS is a terrible language lmao
5
4
Oct 12 '20
[removed] — view removed comment
4
u/draq100 Oct 12 '20 edited Oct 12 '20
you can consider the extra
+as casting to Number type in JSso, first
+means 'add', second here is 'cast'
'a' + + 'a'is equivalent of'a' + Number('a')you can check it out by typing
typeof +'a'(ortypeof +'anything', really) -> it returns"number", NaN in this caseedit: yes, in JS
NaN- "Not a Number" is a number, too1
u/ricealexander Oct 12 '20
The first plus sign is a concatenation operator, which joins strings together.
The second plus sign is a Unary plus operator, which coerces the following expression into a number.
9
u/nizzok Oct 12 '20
This is a JS dad joke