1

TypeScript transpiles certain code into this:

Animal.prototype.move = function (distanceInMeters) {
    if (distanceInMeters === void 0) { distanceInMeters = 0; }
    ...

What's void 0? Is that the same trick as used for links void(0)? Why is not undefined used instead?

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277

2 Answers2

5

The void operator always evaluates as the undefined value.

The undefined variable, which defaults to holding the undefined value, can be overwritten.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
3

The void operator evaluates the given expression and then returns undefined.

The void operator is often used merely to obtain the undefined primitive value, usually using “void(0)” (which is equivalent to “void 0”). In these cases, the global variable undefined can be used instead (assuming it has not been assigned to a non-default value).

Manikandan
  • 182
  • 1
  • 10