javascript - (function(){ foo="bar";})();

I saw some programmers use something like this in their code:

(function(){
   foo="bar";
})();

Why would I use this while I can just replace it all with let foo="bar"; ?

Edit

I saw this in discord.io library they used the following:

(function() {
Payloads = {
    IDENTIFY: function(client) {
        return {
            op: 2,
            d: {
                ........
            }
        };
    },
  ....

And then they called the Payloads.IDENTIFY(...) from outside the mentioned function