JSFiddle - React, Tailwind, and code Playground
by alirokni
JavaScript
//http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html
var count = 1;
alert(count) // 1
if (true) {
// function alertMeAgain(){
var count = 2;
alert(count); //2
// };
}
//alertMeAgain()
alert(count); //2, with function back to 1
//http://msdn.microsoft.com/en-us/library/ie/bzt2dkta(v=vs.94).aspx
var aNumber = 100;
tweak();
function tweak() {
// This prints "undefined", because aNumber is also defined locally below.
alert(aNumber);
if (false) {
//(function(){ // to make it execute and set the value to 100
var aNumber = 123;
//})()
}
}
var name = "Richard";
function showName() {
var name = "Jack"; // local variable; only accessible in this showName function
alert(name); // Jack
}
alert(name); // Richard: the global variable
// Finally it is
var count = 0;
alert("1th " + count); // 0
//if (!count) {
var count = 1;
alert("2nd " + count); // 1
function test() { // closure
var count = 0;
return "3rd " + count; // 0
}
//}
alert("4th " + count); // 1
alert(test());