0

Why isn't a good practice put void(0) when a function doesn't return anything explicitly?

function someFunc(a) {
  // do some stuffs
  return void(0);
}

This would help very much when debugging stuffs.

Thanks.

  • I could be oversimplifying, but `void(0)` evaluates as `undefined`. A function that is defined without an explicit return statement (or simply `return;`) implicitly returns `undefined` anyway, so, the explicit return is redundant. Take a look at http://stackoverflow.com/questions/20915450/why-javascript-functions-always-return-a-value. – Cᴏʀʏ May 25 '16 at 02:29
  • yeap, but i don't think this is redundant, cause would be debugging, don't u think? functions always return something, so this would help, i don't know, maybe i'm wrong. –  May 25 '16 at 02:33
  • `function someFunc(a) { }` and `function someFunc(a) { return; }` and `function someFunc(a) { return void(0); }` are all functionally equivalent. I think once you know that's how it works, you can comfortably stick with the shortest version, with no explicit return statement. – Cᴏʀʏ May 25 '16 at 02:35
  • would be a bad thing use void(0) to helping in debug? i think it really helps. –  May 25 '16 at 02:41
  • 1
    *void* is an operator, not a function, so the brackets are redundant, `void 0` is sufficient. It's used as a short way to write "undefined", but in a return statement it's pointless as a return statement without a value returns *undefined* anyway. – RobG May 25 '16 at 02:42
  • @D_REIS: Show me an example; I'm not convinced. There's no point in explicitly writing out what the JavaScript compiler is automatically going to compile into your functions for you. – Cᴏʀʏ May 25 '16 at 02:42
  • @Cory, exactly. @RobG, both `void 0` and `void(0)` are valid syntax. – Himmel May 25 '16 at 02:44
  • @Himmel—I didn't say it was invalid syntax, just pointless use of a grouping operator, like `x = (1 + 2);` – RobG May 25 '16 at 04:06
  • I see, that's right. – Himmel May 25 '16 at 04:07
  • "*when a function doesn't return anything explicitly?*" - because if you want to **implicitly** return undefined, you shouldn't explicitly `return` *anything*. – Bergi May 25 '16 at 04:13

1 Answers1

0

Since void(0) resolves to undefined, the question could just as well be "why isn't it a good idea to return undefined from a function?". Using void(0) as an alternative to undefined is probably a bad idea, relatively speaking, because it's more complex than undefined, and less explicit. Our goal as programmers is to make simple, readable code for others to read and make sense of.

It's a bad idea to explicitly return undefined from a function for the same reason, it introduces unnecessary complexity. typeof undefined evaluates to undefined. This makes writing tests more difficult and gives you less insight into the purpose of a function than does, say, returning false.

Himmel
  • 3,629
  • 6
  • 40
  • 77
  • I'm not condoning its usage, but I think one reason `void(0)` exists is to always return a true `undefined`, since in some contexts you can override the value of `undefined`. – Cᴏʀʏ May 25 '16 at 02:37
  • i think you are wrong Himmel, is better to use void(0) because of incompatible issues with older browsers with undefined, like Cory said. –  May 25 '16 at 02:39
  • `void(0)` is equivalent to `undefined` when you are returning a value. If either of those are assigned to a variable, well, both of those can be overwritten by any value. – Himmel May 25 '16 at 02:40
  • @D_REIS, your statement contradicts itself and makes the case in my favor. You should use `undefined` because older browsers are incompatible with it..? – Himmel May 25 '16 at 02:41
  • `typeof undefined` returns the string "undefined", not the undefined value. – RobG May 25 '16 at 02:45
  • @RobG where did I indicate otherwise? – Himmel May 25 '16 at 02:47
  • @Himmel—where you said "`typeof undefined` evaluates to `undefined`". It doesn't, it evaluates to the string "undefined". E.g. `if (typeof undefined)` always resolves to true since the string "undefined" is truthy. – RobG May 25 '16 at 04:10
  • Both make for poor function design, in my opinion. – Himmel May 25 '16 at 04:11