-3

At the moment I do a course about JavaScript on codecademy.

Now I got to a task where i need to create a function in a variable. Can anyone explain to me why i can do functions in a variable?

I don't know why I should be able to do this. After all I can call the function with it's own name.

Big thanks for every helpful answer!

Byakko_Haku
  • 639
  • 1
  • 9
  • 25
Nyxero
  • 69
  • 10
  • 1
    That is beauty of JavaScript ! Many other languages has this feature. What is the problem with that? –  Nov 04 '16 at 09:00
  • There's no difference between a string, a number, *a function* or other types in Javascript. They're all simple values and can be assigned to variables. That's all. – deceze Nov 04 '16 at 09:01
  • There's nothing special, a function declared as statement (that requires identifier) is declared as first variable of its scope, FYI – Klaider Nov 04 '16 at 09:01
  • Well, by the same logic you could ask "why a number in a variable" or "why a string in a variable". After all you can use the number/string directly. – JJJ Nov 04 '16 at 09:02
  • 1
    http://stackoverflow.com/questions/336859/javascript-function-declaration-syntax-var-fn-function-vs-function-fn – Psylogic Nov 04 '16 at 09:06

1 Answers1

2

This is because javascript supports first class functions, or in other words, functions in javacsript are objects. So you can pass them to a variable, pass them as argument to a function, return them in a function, etc.

I hope this will be helpful. If not, tell me.

Hicham
  • 81
  • 5