+ 4
Why does this code output the whole thing instead of 6
15 odpowiedzi
+ 4
it's because even functions in JavaScript are treated as objects
+ 4
var aaaa=function(){
return 6;
}
alert(aaaa());
+ 4
chaoticDawg I know that that will work but I was thinking only this
aaaa="function(){return 6}"
should produce such an output
+ 4
Morpheus alert(typeof aaaa) is function not object!
+ 4
Morpheus I'll study this on Google,its confusing
+ 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.
+ 4
☝, that's y functions can behave like variables in JavaScript, or in ur case as a string
+ 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
+ 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()
+ 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
+ 1
https://code.sololearn.com/Wkk9pu1e5cx7 check this out
guyz
+ 1
@Petr we can, why not?
+ 1
@01ejka sorry, my mistake. JavaScript is not my first and even second language :)
0
If I'm not mistaking, you can't write a function to a variable in JavaScript