DEV Community

Discussion on: Pragmatic types: types vs null

Collapse
 
tux0r profile image
tux0r

A variable that is undefined does not exist. Yes, you can use somefunction(variable_you_have_not_declared_yet) - but I would argue that the variable_you_have_not_declared_yet which has a "value" of undefined lacks any form of non-implicit existence.

Thread Thread
 
stereobooster profile image
stereobooster • Edited

To make sure we use the same terminology

A variable that is undefined does not exist

A variable that doesn't exist is an error of a different kind, this is not the same as an uninitialized variable. For example JS:

a
VM54:1 Uncaught ReferenceError: a is not defined
    at <anonymous>:1:1
(anonymous) @ VM54:1

Flow:

a   
^ Cannot resolve name `a`.

TypeScript:

a
Cannot find the name 'a'.

On the other hand, an uninitialized variable is an existing variable which doesn't have value. It can have no value for two reasons:

  • it wasn't assigned a value at the moment of creation, and will be assigned later, like let a;
  • it was assigned with the result of an operation which can return null-value, like let a = array[0]; (undefined in case of JS)

In my opinion, the destination between null and undefined is over-complication. They both represent the absence of value. undefined was introduced for historical reasons, I believe this is just an oversight in the language design (see the post by Dr. Axel Rauschmayer). The same way as Tony Hoar says that null was a mistake in AlgolW design.