javascript: determine functions return type
No, you are going to have to run the function and check the type of the resulting value.
This isn't Haskell - Javascript functions can return anything.
function dosomething()
{
return true;
}
var myfunc=dosomething();
if(typeof myfunc=="boolean") alert('It is '+typeof myfunc);
You can use
if(typeof myfunc=="boolean") or if(typeof myfunc=="number") or if(typeof myfunc=="string")
or
if(typeof myfunc=="object") or if(typeof myfunc=="undefined") to determine the type.
Check what the type is:
var x = typeof doSomething2('a');
if (x == "string")
alert("string")
else if (x == "number")
alert("number");
else if (x == "undefined")
alert('nothing returned');
else if (x == "boolean")
alert("boolean");
else
alert(x);
A simple shorthand would of course be, assuming "undefined"
would be fine to return instead of "nothing returned"
:
alert(typeof doSomething2('a'))
Example use:
[undefined, 'stringyMeThingy', 42, true, null].forEach(x => console.log(typeof x))