DanielM DanielM - 3 months ago 8
TypeScript Question

Why assignments don't return undefined?

As you may know, JavaScript returns

undefined
for assignments, e.g.
var a = 0;
. However, TypeScript is not consistent with that fact, and seems to return the assigned value (
0
) – yet I could not find any information about this online.

As an example, see this code snippet in the TypeScript playground.

I would like to learn the reasons behind this decision, and to hear opinions about it.

Answer

Your code snippet contains return (l = 2), not return (var l = 2), the former will return 2 in JavaScript, TypeScript, or indeed C or Java.