JavaScript traps · JavaScript

JavaScript: "b"+"a"+ +"a"+"a" = baNaNa?!FREE

Watch the video

The extra + turns "a" into a number, and "a" is not a number: NaN lands in the middle of the word.

// "b" + "a" + +"a" + "a" = "baNaNa" ?!
// The extra + in front of "a" turns it into a NUMBER, and "a" is not a number: NaN.
console.log("b" + "a" + +"a" + "a");   // baNaNa ?!

console.log(+"a");      // NaN: unary + converts to a number
console.log(+"42");     // 42: works when the text IS a number
console.log("b" + "a" + "n" + "a" + "n" + "a");   // banana ✓
Open code JS7 in Pocket Compiler Download the Pocket Compiler app — free

No app yet? Install it, then open the app and type the code JS7 in Code Library.