Var promotion problem

var tmp = new Date();

function f() {
  console.log(tmp);
  if (false) {
    var tmp = "hello world";
  }
}

f(); // undefined
var tmp = new Date();

function f() {
  console.log(tmp);
 
}

f(); // Tue Feb 26 2019 17:41:29 GMT+0800 ()

this obviously if will not execute is equivalent to no reason whether to write the output is different

Jul.12,2022

var tmp = new Date();

function f() {
  console.log(tmp);
  if (false) {
    var tmp = 'hello world';
  }
}

f(); // undefined




var tmp = new Date();

function f() {
  var tmp;
  console.log(tmp);
  if (false) {
    tmp = 'hello world';
  }
}

f(); // undefined

var only has function scope, the query process is regarded as scope first, if not continue to look up!

refer to https://codeshelper.com/a/11...

.

during the call to a function, the execution context is created and the life cycle is divided into two phases, creation and execution.
creation phase: the execution context creates variable objects, establishes scope chains, and determines the direction of the this, respectively.
execution phase: variable assignments, function references, and other code execution are completed.

that is, if a variable declaration is found during the creation phase, an attribute is created with the variable name and the value undefined. The execution phase is really assigned.

Source: Front-end basic advance (3): detailed explanation of variable objects


you already know the concept of ascension, but you don't know the answer yet. Promotion occurs in the parsing phase, no matter whether the code is executed or not, the promotion will occur


pre-compilation, there is a quartet, you will understand.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e883ec-ebe.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e883ec-ebe.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?