Skip to content
This repository was archived by the owner on Mar 2, 2024. It is now read-only.

Null and Undefined types

zVictor edited this page May 8, 2013 · 2 revisions

Both Undefined and Null types are commonly misunderstood, so before we explain how ArgueJS treat them, we need to define them:

Definition

In short:

  • undefined is where no notion of the thing exists; it has no type, and it's never been referenced before in that scope;

  • null is where the thing is known to exist, but it's not known what the value is.

It is easier to understand if we have in mind the difference between a value and a variable.

A variable is a storage location that contains a value.

This way:

  • null means that the storage location does not contain anything ( but null itself is just a special kind of value ).
  • undefined means that the variable ( as opposed to the value ) does not exist.

Implementation

Null and Undefined are the only 2 JavaScript values that do not have object equivalents which wrap around the primitive values, e.g. a String object wraps around a string primitive.

Usage

undefined

undefined is used by JavaScript and means "no value". Uninitialized variables, missing arguments and unknown variables have that value.

The following snippet shows us that we usually should not use undefined in our code to mean "no value" just because we are meaning that no notion of the thing exists:

> var myArray = [undefined];
> myArray[0]
undefined
> myArray[9999]
undefined
> myArray[9999] === myArray[0]
true

null

null is used by programmers to indicate "no value", e.g. as a argument to a function.

null is not an Object, it is a primitive value. For example, you cannot add properties to it.

Sometimes people wrongly assume that it is an Object, because typeof null returns "object". But that is actually a bug ( that might even be fixed in ECMAScript 6 ).

Using null in our code we mean the variable is known to exist, but it's not known what the value is:

> var myArray = [null];
> myArray[0]
null
> myArray[9999] === myArray[0]
false

ArgueJS and Undefined/Null types

When a value belongs to types Undefined/Null?

Both have only one possible value for each type.

A value belongs to Undefined type only when its value is undefined, that means value === undefined, and a value is Null only when its value is null, which means value === null.

> __.belongs(undefined, undefined)
true
> __.belongs(null, undefined)
false
> __.belongs(undefined, null)
false
> __.belongs(null, null)
true

Parameter type-checking

Undefined

As a undefined value means no notion of the thing exists, it makes no sense to force a argument to be typed as not existent.

> function fn(){
  arguments = __({param: undefined});
}
> fn()
TypeError: unsupported type undefined

Null

As a null value means not known what the value is, it sounds natural to use it as our wildcard to accept any type.

That is exactly what we do to avoid arguments type-checking

> function fn(){
  arguments = __({anything: null});
  return arguments.anything;
}
> fn("Hello World!")
"Hello World!"

Why null is not accepted as Object argument?

Just because null is not an object.

Sometimes people wrongly assume that it is an Object, because typeof null returns "object".

References

Some parts of this text are quotations from the following references and we are grateful for the respective authors: Rob, Rodrick Chapman and Axel Rauschmayer