Last night, I was scrolling through twitter and I found a interesting JavaScript question, and I bet you won't be able to answer this without cheating.
Let's see how long would would it take for you to answer this one:
What does ("b" + "a" + + "a" + "a").toLowerCase()
output in JavaScript?
The question does look simple, but the answer will definitely blow your mind.
Tell me how long it took you to answer in the comments.
And Yes, JavaScript is crazy!
Top comments (29)
I wanted to see if ChatGPT will get it right.. And it did :)
The NaN part is very interesting, as one of the "a" chars is dropped, initially, I didn't understand why it's not "bananaa" (extra "a"), now I get it 💪🏻
banana
What!! No wayy, how is it banana
Where does the n come from??
Unary plus on "a" gives you
NaN
Now try
console.log(new Array(4).join(Number('y tho') + 'a')+' Batman!')
🦇NaNaNaNaNaNa batman!
is that the desired output or am I missing something? 🤔Yes, maybe for people from my generation 😅.
Now I think this joke is too old 👴
Haha, maybe that joke is from the 'dad joke' era! 😄😄.
Hey, I know this is bit out of context, is there any way I could message you, I see you are a senior dev, I am at an early stage of my career, and I would love some guidance.
Its actually banana, basically the unary plus(++) of 'a' gives NaN which gives us 'baNaNa' and convert it to lowercase => banana
Do you need
at the top of the script 🤔
I used an online Javascript console.
Try this code :
Thank you; my mistake.
I didn't see the ++.
For my defence on my phone it red:
yep, that' s what it does.
NaN is actually part of IEEE 754, which is used in most languages to express floating point numbers.
That's honestly cool.
and weird
Okay the person who thought of this problem is soooo clever
JavaScript is really a magical language
when it gets to insane questions...
banana
Is it bAaa
✅✅
In your case it's bananaa
It's
banana
now i see why typeScript and them exist.
obviously it is string :"b"+string :"a"+NaN+string :"a".
Do not ask me about the last string :"a".
let us say it is your homework.
It took me a second because I didn't notice the ++ (Unary plus) right away. Definitely got a little laugh out of me once I caught it though.