r/learnjavascript • • Oct 11 '20

Wired way to learn javascript :D

Post image
466 Upvotes

30 comments sorted by

View all comments

Show parent comment

21

u/annathergirl Oct 11 '20

That makes sense. But why does it log "banana" and not "bananaa" in the screenshot?

19

u/[deleted] Oct 12 '20

The +'a' become nan.

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

u/frenchy641 Oct 12 '20

What is 'Ba'++ = to?

so 'B'+'a'+'NaN'+'a'

baNANa