JS Bits with Bill
JS Bits with Bill

JS Bits with Bill

The JavaScript Banana šŸŒ

The JavaScript Banana šŸŒ

JS Bits with Bill's photo
JS Bits with Bill

Published on May 21, 2021

1 min read

You gotta make programming fun once in a while. Why not have a banana!

('b'+'a'+ +'šŸŒ'+'a').toUpperCase();
// BANANA

WTF! šŸ™‰ Depending on the context, + in JavaScript will function as the unary operator to convert an operand into a number. The first plus sign is used to concatenate the "b" and "a" strings while the third is used to convert šŸŒ to a Number type - and this returns NaN. Add a trailing "a", use another + to concatenate "ba" + NaN + "a", set it to all caps, and we get BANANA!

Ā 

Bonus Shenanigans

See what this returns in the Chrome console!

const p1 = (+'šŸ¦‡'+'a'+ +'šŸ‘Ø'+'a').repeat(2);

const str = window.BatteryManager.toString();
const p2 = str.slice(9, 19).replace('tery', '');

console.log(`${p1} ${p2.toUpperCase()}!`);

Check out more #JSBits at my blog, jsbits-yo.com. Or follow me on Twitter!

Ā 
Share this