+ 2

Javascript Question

Why do we have to declare a JS function inside a variable? Eg: var a = function name() {} Can we not declare it like this? function name() {}

19th Aug 2020, 5:44 PM
Harshit Srivastava
Harshit Srivastava - avatar
3 Respostas
+ 1
In first type you need not to mention name Like var a = function (){ } . You can call it as a(); It's called annonymous function and more secure. Second type is normal function you can call it as name();
19th Aug 2020, 5:56 PM
Divya Mohan
Divya Mohan - avatar
+ 4
Yup, u can do both.
19th Aug 2020, 5:48 PM
maf
maf - avatar
+ 1
Divya Mohan Thanks!!
19th Aug 2020, 6:13 PM
Harshit Srivastava
Harshit Srivastava - avatar