Hack-Reactor part 2: Hoisting

part 2: Hoisting

by ndw5015

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

JavaScript

//variable declarations hoisted to the top
//function declaration are hoisted to the declaration
//all hoisting is done to the top of the current scope
$( document ).ready(function() {

	// first example
		var msg, f;
    
    msg = 'Holy cow this is interesting';
    //console.log('first log' + ' ' + msg); //first console log
    
    f = function(){
    	var msg;
    	//console.log('second log' + ' ' + msg); //second console log
      msg = 'Not as intersting as it is over here';
      //console.log('third log' + ' ' + msg); //third console log
    }
    f();
    
    //console.log('fourth log' + ' ' + msg); // fourth console log
    
   //second example
   //dont use the same name twice
   var myName;
   
   function myName(){//myName is already defined
   	console.log('hack reactor');
   }
   
   //function declaration 
   console.log(typeof myName); //typeof will show the type 
    
});