Variable declaration promotion

  • Variables defined (declared) by var are accessible before statements are defined
  • Value: undefined
      console.log(a) // undefined
      var a = 4
Copy the code

Function declaration promotion

  • Functions declared by function can be called directly before
  • Value: Function definition (object)
    fn()
    function fn() {
      console.log('fn()') // fn()
    }
Copy the code

Questions and Reflections

  • What does it output?
    var a = 3
    function fn() {
      console.log(a) // undefined
      var a = 4
    }
    fn()
Copy the code
  • What does it output?
    fn() // The function declared by function can be called directly before
    var fn = function () {
      console.log('fn')}Copy the code