Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between JavaScript functions? [duplicate]

I use only jQuery for writing JavaScript code. One thing that confuses me is these two approaches of writing functions,

First approach

vote = function (action,feedbackId,responseDiv)
{
    alert('hi');
    return feedbackId;
}

Second approach

function vote(action, feedbackId,responseDiv)
{
    alert('hi');
    return feedbackId;
}

What is the difference between the two and why should one use the first approach or the second approach?

like image 240
Gaurav Sharma Avatar asked Sep 27 '26 22:09

Gaurav Sharma


1 Answers

The first is a function expression assigned to the vote variable, the second is a function declaration.

The main difference is that function statements are evaluated at parse time, they are available before its declaration at runtime.

See also:

  • Named function expressions demystified (article)
  • Explain JavaScript’s encapsulated anonymous function syntax
like image 90
Christian C. Salvadó Avatar answered Sep 29 '26 12:09

Christian C. Salvadó



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!