IIFE strange behavior linked to variables declared with let or var

Advertisements I try to understand why the variable ‘Permissions’ will be typeof ‘function’ when Permissions declared with var but typeof ‘object’ when declared with let (which is the excepted behavior) With ‘var’ – Permissions is typeof ‘function’ var Permissions; (function (Permissions) { Permissions[Permissions[“ADMIN”] = 0] = “ADMIN”; Permissions[Permissions[“READ_ONLY”] = 1] = “READ_ONLY”; })(Permissions || (Permissions… Read More IIFE strange behavior linked to variables declared with let or var

How do I call an array within an IIFE using a forEach loop?

Advertisements I have an array that’s inside an IIFE, then I have a forEach loop that iterates over those array items but I don’t know how to then call the function using the forEach loop. //IIFE – Immediately Invoked Function Expression let pokemonRepository = (function () { //List of Pokemon Characters let pokemonList = [… Read More How do I call an array within an IIFE using a forEach loop?

Returning inside an IIFE function used in setInterval

Advertisements setInterval( function returnval(){ console.log(‘hello world’); return returnval; }(),2000); First of all, keep in mind that I am new to javascript. Can someone please explain this given piece of code that is confusing me? What is actually happening when we return the function name inside an IIFE function contained inside an anonymous setInterval? And also… Read More Returning inside an IIFE function used in setInterval

is const variable in javascript function initialized every time when it called or just once?

Advertisements I’d like to code something like this. function cokeDispencer(id) { const dict = {1:"sprite", 2:"pepcy", …} // Some JSON. It might be bigger. return dict[id]; } Of course, dict[id] is much simpler way to do the same, but I want put the dict object inside of function so that no other function can access… Read More is const variable in javascript function initialized every time when it called or just once?

Why does declaring a variable using googleSheets.spreadsheets.values.get only work as an IIFE?

Advertisements Context: this is not a request for help, I found the solution, I just want to understand why. Working code: var username_result = (await googleSheets.spreadsheets.values.get({ auth, spreadsheetId, range: "users!A" + selrow, })).data; This returns the expected object, and I can log it to console to view its contents. If I remove the outer set… Read More Why does declaring a variable using googleSheets.spreadsheets.values.get only work as an IIFE?

IIFE vs noraml functions, Behave differently regarding managing variables in the global scope

Advertisements Why in Example 1 and Example 2 return different results? A conter variable(example 1) instance still has access from the global scope. Example 1: const increment = (()=> { let counter = 0; //console.log(counter); const credits = (num) => console.log(`I have ${num} credit(s).`); return () => { counter++; credits(counter); } })(); increment();//I have 1… Read More IIFE vs noraml functions, Behave differently regarding managing variables in the global scope