DEV Community

Discussion on: Fictional Q&A about JavaScript Hoisting

Collapse
 
howtocodejs profile image
HowToCodejs • Edited

To add on to what I wrote, you can also re-assign to a variable that contains an anonymous function. You can take advantage of this by writing an if statement that checks if we are in a certain mode


var strictMode = true;

var logNumber = function(num){
  return num;
}

if(strictMode){
  logNumber = function(num){     
    if(isNaN(num)){
      return; // do nothing
    }else {
      return num; // return number 
    }
  }
}

We would've had to have declared two separate functions if we hadn't created a function expression. Still, the difference is minimal.