less than 1 minute read

Case 1. var the variable is not hoisting outside of IIFE scope.

(function() {
  // var is hoisting inside scope.
  for(var i=0; i<10; i++) {
    console.log('i', i)
  }
})()
console.log('after loop i is', i) // ReferenceError: i is not defined

Case 2. without var the variable is hoisting outside the IIFE.

// var i is accessed outside of scope
(function() {
  for(i=0; i<10; i++) {
    console.log('i', i)
  }
})()
console.log('after loop i is', i) // after loop i is 10

Case 3. With use strict the variable i is not hoisting outside of scope.

(function() {
  'use strict'
  for(i=0; i<10; i++) {
    console.log('i', i)
  }
})()
console.log('after loop i is', i) // ReferenceError: i is not defined

Comments