+ 4

Why does this code output the whole thing instead of 6

Why?? https://code.sololearn.com/Wu1oDWL4JubV/?ref=app

24th Dec 2017, 5:52 PM
᠌᠌Code X
᠌᠌Code X - avatar
15 odpowiedzi
+ 4
it's because even functions in JavaScript are treated as objects
24th Dec 2017, 6:02 PM
Morpheus
Morpheus - avatar
+ 4
var aaaa=function(){ return 6; } alert(aaaa());
24th Dec 2017, 6:03 PM
ChaoticDawg
ChaoticDawg - avatar
+ 4
chaoticDawg I know that that will work but I was thinking only this aaaa="function(){return 6}" should produce such an output
24th Dec 2017, 6:07 PM
᠌᠌Code X
᠌᠌Code X - avatar
+ 4
Morpheus alert(typeof aaaa) is function not object!
24th Dec 2017, 6:08 PM
᠌᠌Code X
᠌᠌Code X - avatar
+ 4
Morpheus I'll study this on Google,its confusing
24th Dec 2017, 6:11 PM
᠌᠌Code X
᠌᠌Code X - avatar
+ 4
If you wrote the function in the normal standard way: function aaaa() { return 6; }; alert(aaaa); you'd get the same output in which it prints the entire function instead of the value returned from it. As @Morpheus stated function is also a valid type in Javascript. The definition of the function is its value. When you don't use the parens () to call the function you will get the functions value, as with any other variable, instead.
24th Dec 2017, 6:13 PM
ChaoticDawg
ChaoticDawg - avatar
+ 4
☝, that's y functions can behave like variables in JavaScript, or in ur case as a string
24th Dec 2017, 6:13 PM
Morpheus
Morpheus - avatar
+ 4
looks like it reads the whole thing as a sort of string value and prints it to console.. as var and the variable name isnt printed i could be wrong tho i know nothing about js lol
24th Dec 2017, 6:16 PM
D_Stark
D_Stark - avatar
+ 3
note that typeof for functions returns function , but functions are treated as objects in JavaScript and can be checked by , using object functions like toString()
24th Dec 2017, 6:05 PM
Morpheus
Morpheus - avatar
+ 2
if we use a variable 'a' it will be a function, if we use a function 'a()' it will be a number(as we return a number in a func), funny:) https://code.sololearn.com/Wc444i4ExOdk/?ref=app
24th Dec 2017, 6:48 PM
Botol
Botol - avatar
+ 1
https://code.sololearn.com/Wkk9pu1e5cx7 check this out guyz
24th Dec 2017, 6:20 PM
rahul negi
rahul negi - avatar
+ 1
@Petr we can, why not?
24th Dec 2017, 6:41 PM
Botol
Botol - avatar
+ 1
@01ejka sorry, my mistake. JavaScript is not my first and even second language :)
24th Dec 2017, 7:25 PM
Petr Leliaev
Petr Leliaev - avatar
0
If I'm not mistaking, you can't write a function to a variable in JavaScript
24th Dec 2017, 5:59 PM
Petr Leliaev
Petr Leliaev - avatar