Is an anonymous function expression? if it is, why can not we use it wherever JavaScript expects an expression?
Example :
const result = String(function () {
return 2;
});
what variable returns :
'function () {\r\n return 2;\r\n}'
but I expected to get 2 in string format because if anonymous function is expression it must return 2 as the argument for the String function
I tried to use an anonymous function where js expects an expression but i did not get the result what i expected
You forgot to invoke the function. A function definition by itself is just that, a definition. If it's never invoked, it won't return the value.
(And interpreting a function definition as a string results in the text of the function, which is the result you're observing.)