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

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

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 thank… 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?

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 to… 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?

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 of… 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

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 credit(s).… Read More IIFE vs noraml functions, Behave differently regarding managing variables in the global scope