// 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.
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
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.
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Actually,
undefined
is not a secondnull
value.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
andJSVAL_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
A variable that is undefined does not exist. Yes, you can use
somefunction(variable_you_have_not_declared_yet)
- but I would argue that thevariable_you_have_not_declared_yet
which has a "value" ofundefined
lacks any form of non-implicit existence.To make sure we use the same terminology
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:
Flow:
TypeScript:
On the other hand, an uninitialized variable is an existing variable which doesn't have value. It can have no value for two reasons:
let a;
let a = array[0];
(undefined
in case of JS)In my opinion, the destination between
null
andundefined
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.