Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does Banana is printing in JavaScript?

Today I was trying random letters printing using nodejs, Somehow I tried to print ‘banana’ in log but unfortunately I miss n (letter) to log but still it works fine. Why does it prints whole banana instead of ‘baaa’?

('b' + 'a' + + 'a' + 'a').toLowerCase();

The output is banana but why? Even if + + (empty char) generates NaN then still it should print bananaa not just banana.

Screenshot:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

>Solution :

The extra + acts as a unary operator on the following ‘a’, and tries to coerce it to a number, resulting in NaN. The remaining + symbols are all interpreted as string concatenation which causes NaN to be coerced to string ie. ('b' + 'a' + + 'a' + 'a') = ('b' + 'a' + NaN + 'a') = ('baNaNa').

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading