DEV Community

Nazmul Hossain
Nazmul Hossain

Posted on

Javascript Banana

In JavaScript, the code "b" + "a" + +"a" + "a" returns "baNaNa".
Confused?
Let's break it down.

The first two parts of the expression, "b" + "a", simply concatenate the strings 'b' and 'a', resulting in the string 'ba'.

The third part, +"a", is where things get interesting. The plus sign preceding the 'a' is a unary operator that converts the string 'a' into a number. In this case, the string 'a' is coerced into the number NaN (Not a Number).

Finally, the last part of the expression, "a", is simply another string that is concatenated to the previous parts.
When we put it all together, we have "ba" + NaN + "a", which results in the string 'baNaNa'.

This code snippet is a great example of the type of coercion that occurs in JavaScript and how it can sometimes lead to unexpected results. As developers, it's essential to carefully consider the types of variables and expressions in code to avoid these types of surprises.

Top comments (0)