Explain the original data types in JavaScript null and Undefined_javascript tips

Source: Internet
Author: User
Tags garbage collection

When discussing the raw data types in JavaScript, most people know the basics, from String,number to Boolean. These primitive types are fairly simple and behave in a common sense. However, this article will focus more on the unique original data types null and undefined, what makes them so similar, but specious.

I. Understanding null and undefined
In JavaScript, NULL is a literal and is also a keyword in the language, used to represent unrecognized object values. In other words, this is used to indicate "no value". Although similar, undefined actually represents nonexistent values (Non-existence of a value). Are completely immutable, have no properties and methods, and cannot assign values to their properties. In fact, attempting to access or define a property will throw a type error (TYPEERROR). As their name implies, they are completely invalid values.

A Boolean value that is not represented by a value is false, which means that they are evaluated as false in the context of the condition, such as an if statement. Using the equality operator (= =) to compare these two values and other false values, they are not equal to except themselves:

NULL = = 0; False
undefined = = ""; False
NULL = = FALSE;//false
undefined = = false;//false
null = undefined;//True

However, as with other similarities, null and undefined are not equivalent. Each unique member of its distinct type, undefined is a undefined type and null is a null type. Use the strict equality operator (= = =) To compare these two values, which require both types and values to be equal, as evidenced by the following:

undefined = = NULL; False

This is an important distinction that serves different purposes and reasons. By distinguishing between these two values, you can assume that undefined represents an unexpected representation with no value and null as expected.
ii. generation of undefined
There are many ways to generate a undefined value code. It is commonly encountered when attempting to access a value that does not exist. In this case, in the dynamic, weakly typed language of JavaScript, only a undefined value is returned by default instead of rising to an error.
Any declaration of a variable without providing an initial value will have a default value of undefined:

 var foo; The default value is undefined

Returns a undefined value when an attempt is made to access an object property or array item that does not exist:

var array = [1, 2, 3];
var foo = Array.foo; The Foo property does not exist, returns undefined
var item = array[5];//an item with no index 5 in the array, returns the undefined

If the return statement of the function is omitted, return undefined:

var value = (function () {}) (); Back to undefined

Values that are not supplied at the time of a function call will be undefined parameter values:

(function (undefined) {
  //parameter is undefined
}) ();

The void operator can also return a undefined value. Libraries like underscore use it as a defensive type check because it is immutable and can be returned on any context-dependent undefined:

function isundefined (obj) {return
  obj = = void 0;

Finally, undefined is a predefined global variable (unlike a null keyword) initialized to a undefined value:

' undefined ' in window; True

In ECMAScript 5, this variable is read-only and is not previously the case.

three, null use cases
Null's use case is the main aspect that makes him different, because unlike Undefined,null is considered to be more useful. This is why the TypeOf operator returns "Object" when acting on a null value. The original reason is that it is still, and is often used as an expectation of a null reference to an empty object, like a placeholder. This behavior of TypeOf has been identified as a mistake, although the amendment has been made, and this has remained unchanged for later compatibility purposes.
That's why the JavaScript environment never sets a value to null; it must be done programmatically. As the document MDN says:
in the API, NULL is the place to often retrieve objects, but there are no related objects.
This applies to the DOM, which is language-independent and does not fall within the scope of the ECMAScript specification. Because it's an external API, trying to get a nonexistent element to return a null value instead of undefined.
Generally, if you need to specify an invariant value for a variable or attribute, pass it to a function, or return null,null from a function is almost always the best choice. In short, JavaScript uses undefined and programmers should use NULL. Another feasible use case of
Null is also considered good practice is an explicitly specified variable to be invalid (object= null) when a reference is no longer required. Effectively clears the reference by assigning null values, and assumes that the object does not reference other code, specifies garbage collection, and ensures that the memory is reclaimed.
Four, deep mining
makes null and undefined like black holes not just their behavior, but the way they handle them within the JavaScript environment. They do not usually seem to have the same associated characteristics with other native or built-in objects.
in ES5, the Object.prototype.toString method has become the actual type-checking standard, which is shown to be consistent in null and undefined:

Object.prototype.toString.call (NULL); [Object Null]
Object.prototype.toString.call (undefined);//[Object undefined]

However, the Object.prototype.toString method is not really an internal [[Class]] property or a undefined public constructor that retrieves null. Depending on the document, the following steps occur during the call:

If the value is Undefined, return "[Object Undefined]".

    • If this value is null, then "[Object Null]" is returned.
    • Let o pass the This value as the result value of the argument as the call Toobject.
    • Let Class be the value of the internal property [[Class]] of O.
    • The result of the return connection is a string value of the result of the three string "[Object, class, and]".

The method performs a simple string return value if it detects null or undefined and other object-uniform functionality. This is common throughout the specification, because most methods contain a simple capture and return when null and undefined values are encountered. In fact, there is no indication that they contain internal attributes associated with any native object. It's like they're not objects. I wonder what would happen if an explicit scheme existed inside a JavaScript native environment? Maybe someone is more familiar with a possible implementation that can be involved.

Regardless of how unusual these native objects are, understand the differences between null and undefined, and their distinct roles in the language base of JavaScript. It may not make a breakthrough in your application, but in general it is only proven to be beneficial in development and commissioning.

The above is the original data type in JavaScript for null and undefined of the introduction, I hope to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.