DEV Community

Discussion on: Pragmatic types: types vs null

Collapse
 
tux0r profile image
tux0r

Actually, undefined is not a second null value.

// x is undefined until we set it to something.
let x = null;       // The variable x is defined with no value.
// However:
let y = undefined;  // The variable y does not exist.
Collapse
 
stereobooster profile image
stereobooster

Not sure I get your argument. Variables exist in both cases, but they have no value. (I know that from machine point of view they have two distinct values JSVAL_NULL and JSVAL_VOID)

And this is what null is for - to represent absence of value. We have variable, but it doesn't hold any value. If you want to read more about historical reasons why JS have both null and undefined you can read this post by Dr. Axel Rauschmayer

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.