var funcName = function() {} vs function funcName() {}

2 ways of declaring functions in js:


var functionOne = function() {

    // code

};

function functionTwo() {

    // code

}

The difference is that functionOne is defined at run-time, whereas functionTwo is defined at parse-time for a script block. For example:


<script>

  // Error

  functionOne();



  var functionOne = function() {

  }

</script>



<script>

  // No error

  functionTwo();



  function functionTwo() {

  }

</script>

In JavaScript, declarations are hoisted. Meaning that var a = 1; var b = 2; becomes var a; var b; a = 1; b = 2.

Leave a Comment