scenario

When I was looking at my colleague’s code today, I noticed something very strange

const Father = () = > {return <Son />}
const Son = () = >{... }Copy the code

The Son component is defined after it is used. Since const does not promote variables, why does the code still work

The sample

console.log(name) // undefined
var name = Hermes

console.log(name) // Uncaught ReferenceError: Cannot access 'name' before initialization
const name = Hermes

console.log(name) // Uncaught ReferenceError: name is not defined
Copy the code

Const is not promoted. But the Error message tells you that you can’t use name, but if you don’t define name, you should say not defined. Name will be placed in temporal dead zone, so it can be understood as another form of promotion operation

expand

When a function is promoted at the same time as a variable, the function is promoted first, higher up the list