Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring a JavaScript function that is inside a variable?

I'm reading this tutorial: http://nathansjslessons.appspot.com/

Inside there's a lessons that says:

// A simple function that adds one
var plusOne = function (x) {
    return x + 1;
};

I'm used to see functions like this:

function myFunction() {
    return x + 1;
};

What's the difference between the first and the second?

like image 340
alexchenco Avatar asked Jan 19 '23 07:01

alexchenco


1 Answers

The only difference is the first function:

var plusOne = function (x) {
    return x + 1;
};

is defined at runtime, whereas another function:

function myFunction() {
    return x + 1;
};

is defined at parse time for the script block.

like image 198
Sudhir Bastakoti Avatar answered Jan 31 '23 00:01

Sudhir Bastakoti