Null and undefined in JavaScript

Source: Internet
Author: User

The data types of JavaScript are broadly divided into two categories: primitive types and Object types. Where the original type includes numbers, strings, and Boolean values. In addition, there are two special primitive values in javascript: null (empty) and undefined (undefined), which are not numbers, strings, and booleans, which usually represent unique members of their particular type.

Null

Null is a keyword in the JavaScript language that represents a special value that is commonly used to describe "null values". The typeof budget is executed against NULL, and the result returns the string "Object" (for example), so that null can also be considered a special object value, meaning "non-object". In practice, however, it is generally assumed that NULL is the only member of its own type, which can represent numbers, strings, and objects as "no value".

typeof (NULL); Object

Undefined

Undefined can also be used to indicate a value vacancy. It is not the same as NULL, it is not a keyword, it is a value of the variable, it indicates that the variable is not initialized, and when querying the object's property or the value of an array element returns undefined, it means that the property or element does not exist. If the function does not return any values, undefined is returned. A reference to a function parameter that does not provide an argument will only get undefined. Undefined is a pre-defined global variable whose value is undefined.

null and undefined in the same place

Both null and undefined are completely immutable values, have no properties and methods, and cannot add attributes or assignments to them (because they do not have attributes at all). If you attempt to define a property, a type error (such as) is thrown, and they are completely invalid values.

in the In JavaScript, the Boolean value represented by " No value " is false, which means that they are evaluated as false in the context of the condition, as in the IF statement.

Note: Use the equality operator (= =) Comparing the two values with the other false values is not equal, if the comparison between them is equal, but not congruent (= = =), because the types are different.

question: Since both null and undefined are represented as false in the IF judgment statement, why can't they be equal to false values?

When comparing false objects to other objects with the equals (= =) operator, it is found that only 0 and empty strings equal to false;undefined and null objects are not equal to false objects, and null and undefined are equal. Therefore, 0, empty string and false can be classified as a class, called "false value"; The null and undefined are classified as a class, called "null value". ( Personal Understanding: that is, in JavaScript, false values and null values have a common denominator, that is, if the conditional branch is treated as false; Apply "!" True after the operation)

To illustrate:

If statement, null example shows

If statement, undefined an example of how

null and undefined differences

Null and undefined are basically synonymous, with only a few subtle differences.

1, NULL is an object that represents "not", and a value of 0;undefined is a primitive value that represents "undefined", and a value of Nan when converted to a number.

2,null means "no object", that is, there should be no value at all. undefined means " missing value ", that is, there should be a value here, but there is no definition.

3,null and undefined by typeof to determine the result is not the same.

Note: There is data to show thatthis behavior of TypeOf has been identified as a bug, although the correction was made, for the purpose of post-compatibility, this has remained unchanged.

Extended:

in the In ECMAScript 3, undefined is a read/write variable that can be assigned an arbitrary value. This error was corrected in ECMAScript 5 and undefined is read-only in this version.

Null is often used to explicitly specify that the variable is invalid (object= null) when a reference is no longer required, by assigning a null value, effectively clearing the reference, and assuming that the object does not reference other code, specifying garbage collection, and ensuring that the memory is reclaimed. Sometimes, when a variable is defined, it is initialized to null;

Undefined little or no advice to use anywhere, undefined often appears in many of the method codes. Usually encountered when attempting to access a nonexistent value, only a undefined value is returned by default, instead of rising to an error. Any declared variable does not provide an initial value, and there will be a default value of undefined.

Summary: You can think of undefined as a vacancy for a system-level, unexpected, or error-like value, and NULL is an open program-level, normal, or expected value. If you want to assign them to variables or attributes, or use them as parameter-to-pass functions, the best choice is null.

Null and undefined in JavaScript

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.