why declare java script function in this way? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

why declare java script function in this way?

why do we declare function in this way. first declare a variable then declare a function against the variable. var return_first = function () { var tmp = null; $.ajax({ 'async': false, 'type': "POST", 'global': false, 'dataType': 'html', 'url': "ajax.php?first", 'data': { 'request': "", 'target': 'arrange_url', 'method': 'method_target' }, 'success': function (data) { tmp = data; } }); return tmp; }();

20th Jul 2019, 7:37 PM
Muhammad Idrees
Muhammad Idrees - avatar
1 Answer
0
The javascript language has something called block scope, If you declared a function like: function myFunc() {} it .stays there, the problem is you can't access that function from inside of that function or above it, However the var keyword in javascript get hoisted to the top of your code when you run it. So if you declare a function inside a variable, it will get hoisted to the top of your code and you won't be run into errors later on. Hope this helps! O White
4th Jan 2020, 4:19 AM
O. White
O. White - avatar