JSFiddle - React, Tailwind, and code Playground

by Horia-Adrian Ionescu

JavaScript

/* what will be the console log, and why? */ 
var myVar = 'A';
(function() {
  console.log(myVar);
  var myVar = 'B';
})();
/*
undefined
This happened because this is an immediately invoked function that creates a local scope, and in its own local scope the local"myVar" was not yet defined at the moment of console.log( the variable's declaration is "hoisted" to the top of the function block but it is only initialized after the console.log ). These kinds of function expressions are used a lot by libraries(ex:jquery) in order to not pollute the global namespace with variables used only by it*/