Came across this javascript riddle on Twitter today: https://twitter.com/bradleymeck/status/890795540123865088
// #js
const a = f => f``;
const b = f => f``;
console.log(a(_ => _) === b(_ => _));
// what do you think this will/may print
At first glance it actually seems to make some decent sense. a
is a function that takes some input f
and then does f``
. What f``
is a complete mystery to me so I tossed it into the console and received this input.
(()=>{console.log('hi')})``
hi
So it seems that a trailing template literal executes its preceding function. I understand that template literals are code that is executed immediately, but this behavior makes no sense to me. Can someone explain this to me?