Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript: determine functions return type

Tags:

javascript

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
}
like image 269
Doua Beri Avatar asked Feb 08 '12 21:02

Doua Beri


People also ask

How do you determine the return type of a function?

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.

Do JavaScript functions have return 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.

Do functions return values in JavaScript?

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.


1 Answers

you can use it as a function to be return

const determineFunc = (param) => typeof param;

console.log("logs==>", determineFunc(true));
like image 110
kennethreyt Avatar answered Oct 16 '22 12:10

kennethreyt