Sergey Panfilov - 9 months ago 43

Javascript Question

Is it possible to add

`@return`

`/**`

* @param {Number} a

* @param {Number} b

* @return {Number}

* */

function sum (a, b) {

if (a < b) return false; //here we have a Boolean

return a + b; //and here a Number

}

Note:

- I already know that
**this is bad**. - I
**won't do it**in my code. **Just interested**in how everyone handle this issue (in other ways than refactoring).

Answer Source

There's an example right there in the JSDoc `@returns`

documentation:

The return value can have different types`/** * Returns the sum of a and b * @param {Number} a * @param {Number} b * @param {Boolean} retArr If set to true, the function will return an array * @returns {Number|Array} Sum of a and b or an array that contains a, b and the sum of a and b. */ function sum(a, b, retArr) { if (retArr) { return [a, b, a + b]; } return a + b; }`

E.g., use `|`

, as you do other times the type may vary.