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

Howdy, Stranger!

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

Categories

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

Jeson84Jeson84 USAPosts: 90Member
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?

Tagged:

Comments

  • DavidMDavidM USAPosts: 342Member
    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.