The JavaScript Banana ๐ŸŒ

js_bits_bill

JS Bits Bill

Posted on May 21, 2021

The JavaScript Banana ๐ŸŒ

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

('b'+'a'+ +'๐ŸŒ'+'a').toUpperCase();
// BANANA
Enter fullscreen mode Exit fullscreen mode

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()}!`);
Enter fullscreen mode Exit fullscreen mode

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

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
js_bits_bill
JS Bits Bill

Posted on May 21, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

ยฉ TheLazy.dev

About