Is there a way in javascript to determine the return type(if any) of a function?
example:
function doSomething(){
return true;
}
to returned type is boolean.
example 2:
function doSomething2(x){
if(x=="a") return 1;//number
else return "bad x"; //string
}
function myFunction(a: number, b: number): void { console. log(a); console. log(b); } // 👇️ type T = void type T = ReturnType<typeof myFunction>; If the function might return values of multiple types, its return type will be a union type.
So to recap: No, a JS function needn't return anything as far as your code goes. But as far as the JS engines are concerned: a function always returns something, be it explicitly via a return statement, or implicitly. If a function returns implicitly, its return value will always be undefined.
JavaScript functions can return a single value. To return multiple values from a function, you can pack the return values as elements of an array or as properties of an object.
you can use it as a function to be return
const determineFunc = (param) => typeof param;
console.log("logs==>", determineFunc(true));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With