Basic data types

Before we get to undefined and NULL, let’s take a look at data types in ECMAScript. There are six simple data types (also known as basic data types) in ECMAScript: Undefined, Null, Boolean, Number and String, and Symbol (introduced in ES6). There is also a complex data type called Object.

Undefined and Null have only one value, corresponding to Undefined and Null, respectively. These two different types of values have different semantics and scenarios, but also exhibit relatively similar behavior.

Second, the undefined

Undefined literally means undefined value. The semantics of this value are intended to represent the original state of a variable, not the result of a human operation. This primitive state can occur in four scenarios:

Declare a variable, but do not assign a value

var foo;
console.log(foo); // undefined
Copy the code

Calling foo returns undefined, indicating that the variable has never been used or defined with any valid value since it was declared.

Access properties or undefined variables that do not exist on the object

console.log(Object.foo); // undefined
console.log(typeof demo); // undefined
Copy the code

Call foo on Object and return undefined to indicate that foo does not exist or has no property named foo defined. The Typeof operator on undeclared variables returns undefined.

3. The function defines parameters but does not pass arguments

// The function defines the parameter afunctionfn(a) { console.log(a); // undefined } fn(); // No argument passedCopy the code

The function fn defines parameter a, but fn is called without passing an argument, so fn’s run-time argument A is a primitive, unassigned variable.

4. Use void to evaluate an expression

void 0 ; // undefined
void false; // undefined
void []; // undefined
void null; // undefined
void function fn(){} ; // undefined
Copy the code

ECMAScript explicitly states that the void operator returns undefined for any expression evaluated. This is just like a function that does not return a value. JavaScript functions always return a value. This value is undefined, indicating that the return value of the function is undefined.

Therefore, undefined usually comes from the original state value of an expression, not the result of human manipulation. Of course, you can assign undefined to a variable manually, but that doesn’t make sense because a variable that isn’t assigned is undefined.

Third, null

The literal meaning of null is null. The semantics of this value are intended to indicate that an object has been artificially reset to an empty object, rather than the original state of a variable. The in-memory representation is that a variable in the stack does not refer to an in-memory object in the heap.

1. There are two general situations in which we assign a variable to null

  • If you define a variable that will be used to hold objects in the future, it is best to initialize that variable to NULL rather than some other value. In other words, as long as a variable intended to hold an object does not actually hold an object, it should be explicitly made to hold a null value, which helps to further distinguish null from undefined.

  • When data is no longer needed, it is best to release a reference to it by setting its value to NULL, a practice called dereferencing. However, dereferencing a value does not automatically reclaim the memory used to change the value. The real purpose of dereferencing is to take the value out of the execution environment so that the garbage collector can reclaim it the next time it runs. Dereferencing also helps eliminate the possibility of circular references. This applies to most global variables and properties of global objects, and local variables are automatically dereferenced when they leave the execution environment (when the function completes execution).

2. Special typeof NULL

When we use the Typeof operator to detect null values, we assume that we should return “null”, but the type returned is “object”.

var data = null;
console.log(typeof data); // "object"
Copy the code

Isn’t that weird? In fact, we can understand this result from two aspects:

  • On the one hand, from a logical point of view, null represents an empty object pointer, which in fact represents an empty object, so it makes sense to return “object” when using the Typeof operator.

  • On the other hand, in the original implementation of JavaScript, values in JavaScript were represented by a tag representing a type and the actual data value (the object’s type tag was 0). Since null represents a null pointer (0x00 on most platforms), the type label for NULL also becomes 0, and typeof NULL incorrectly returns “object”. In ES6, there was a proposal that was historically trivial, and the value of type NULL was corrected to NULL, but the proposal was rejected, so the “object” type was kept.

Four,

Null means that a variable has been artificially set to an empty object instead of its original state. Therefore, in practical use, in order to ensure the semantics of the variable, do not explicitly assign undefined to a variable, when you need to release an object, directly assign null.