Is there a way to check if a function returns ANY value at all. So for example:
if(loop(value) --returns a value--) {
//do something
}
function loop(param) {
if (param == 'string') {
return 'anything';
}
}
Functions that don't return an object or primitive type return undefined. Check for undefined:
if(typeof loop(param) === 'undefined') {
//do error stuff
}
function a() {} var ret = a(); // typeof ret === "undefined") and a function which explicitly returned undefined (e.g.: function a() { return void 0; // Returns undefined explicitly } var ret = a(); // typeof ret === "undefined")? Is there a way to know (from the caller side) that a function has returned through a return statement instead of implicitly? Thank youA function with no return will return undefined. You can check for that.
However, a return undefined in the function body will also return undefined (obviously).
undefined return and explicit return undefined) from the caller side?If you want to do something with the output of your function in the case it returns something, first pass it to a variable and then check if the type of that variable is "undefined".
testme("I'm a string");
testme(5);
function testme(value) {
var result = loop(value);
if(typeof result !== "undefined") {
console.log("\"" + value + "\" --> \"" + result + "\"");
} else {
console.warn(value + " --> " + value + " is not a string");
}
function loop(param) {
if (typeof param === "string") {
return "anything";
}
}
}