MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/learnjavascript/comments/j99oon/wired_way_to_learn_javascript_d/g8ktfnx/?context=3
r/learnjavascript • u/ikrum • Oct 11 '20
30 comments sorted by
View all comments
Show parent comments
48
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 of Number('anything') and returns NaN+ '' gives 0 as it's Number('') 21 u/annathergirl Oct 11 '20 That makes sense. But why does it log "banana" and not "bananaa" in the screenshot? 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'
34
('b'+'a'+ +'anything'+'a').toLowerCase() works too
('b'+'a'+ +'anything'+'a').toLowerCase()
part +'anything' is equivalent of Number('anything') and returns NaN
+'anything'
Number('anything')
+ '' gives 0 as it's Number('')
+ ''
Number('')
21 u/annathergirl Oct 11 '20 That makes sense. But why does it log "banana" and not "bananaa" in the screenshot? 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'
21
That makes sense. But why does it log "banana" and not "bananaa" in the screenshot?
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'
37
okay, let's go step by step:
('b'+'a'+ + 'a'+'a').toLowerCase()
becomes (is processed as) ('b'+'a'+ Number('a')+'a').toLowerCase()
('b'+'a'+ Number('a')+'a').toLowerCase()
which becomes ('b'+'a'+ NaN +'a').toLowerCase() (NaN is still a Number here)
('b'+'a'+ NaN +'a').toLowerCase()
which becomes: ('b'+'a'+ String(NaN) +'a').toLowerCase()
('b'+'a'+ String(NaN) +'a').toLowerCase()
after casting to string: ('b'+'a'+ 'NaN' +'a').toLowerCase()
('b'+'a'+ 'NaN' +'a').toLowerCase()
in the end it's ('baNaNa').toLowerCase()
('baNaNa').toLowerCase()
and finally, our result: 'banana'
'banana'
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.