Jessica Carter

Full Stack Developer | Entrepreneur | Life Learner

baNaNa in JavaScript

April 21, 2021

Banana🍌

Banana is a popular secret about javascript which goes like this:

     > "b" + "a" + + "a" + "a"
     // output: "baNaNa"

Here is sample output from playground

image.png

Is this really mysterious?

Honestly telling No, I want you to focus on the javaScripts baNaNa, which goes like: b a NaN a.

Yup, baNaNa is made out of a NaN, let me show it to you visually

image.png

'b' + 'a';
// in Javascript is ba that's normal I guess.

Here comes the twist, the next I will be adding + "a" or + + "a"

image.png

Here is how NaN kicks into the console, that's obvious if you read closely that what I did. I added + "a" to ba which turned to b a NaN

'b' + 'a' + +'a' + 'a';
// turned into
'b' + 'a' + +'a' + 'a';

//    (+"a") is treated as numeric due to an addition symbol,  where a is not a number, (+ "a") === NaN.

That's awesome Right? only happens in the Javascript world.


Profile

Written by Jessica Carter who is a Full Time Developer and writes about tech and life Follow her on twitter