What is the difference between var functionName = function() {} and function functionName() {}

Jeson84Jeson84 USA
edited February 2014 in JavaScript

I came across two types of functions definitions in a JavaScript code.

First

var functionOne = function() {
        // Some code
}

Second

function functionTwo() {
        // Some code
}

Are both of these methods correct? If so in which occasions are these used?

Comments

  • DavidMDavidM USA
    edited February 2014

    There is a difference. functionOne is defined at run time. functionTwo is defined at parse-time for a script block. functionOne will give an error.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion