NULL and undefined distinction and comparison in JavaScript

NULL and undefined distinction between NULL and Undefined

Undefined Type

The undefined type has only one value, that is, special undefined. When using a VAR declared variable but does not initialize it, the value of this variable is undefined,

, for example:

VAR Message; Alert (Message = = Undefined); // true
  This example only declares that the variable Message is not initialized. Compare this variable with the undefined literal, the results show that they are equal. This example is equivalent to the following example:  
var message = undefined; Alert (message == undefined); // true

This example explicitly initializes the variable Message using the undefined value. But we don’t have to do this, because the undefined value will be obtained by default uninterened.
  In general, there is no case where there is a need to be explicitly set to undefined values. The main purpose of the literal undefined is to compare, and this value is not specified in the previous version of the ECMA-262 3rd edition. The third version introduced this value is to formally distinguish between empty object pointers and uninitial variables.  However, variables containing undefined values ​​are still different from those that have not yet been defined. See the example below: 
Var message; // This variable declares defaults to UndeFined value // The following variables did not declare // var Age Alert (Message); // “undefined” Alert (age); // Generate an error

Over run Code, the first warning box displays the value of the variable Message, ie “undefined”. The second warning box – Since the transfer to the Alert () function is not yet declared, it will cause an error. For variable has not been declared, can only perform an action, that the use typeof operator to detect the type of data (call delete undeclared variables not cause an error, but this is no practical significance, and indeed in strict mode It will cause errors).

However, it is confusing that the TypeOf operator is executed to the uninitialized variable returns the undefined value, and the TypeOf operator is also returned to the undecined value. Looking at the example:

// This variable declares after the default obtained undefined value // The following variables are not declared // var Agert (Typeof Message ); // “Undefined”; // “undefined”
   
The result indicates that the TypeOf operator is executed for uninitialized and unspecified variables. Undefined value; this result has its logically rationality. Because although these two variables have essentially different from the technical perspective, virtually no matter which variable is also impossible to perform real operations.
Tip:

Even if the variable that is not initialized will automatically give the undefined value, the explicit initialization variable is still sensible. If you can do this, thenWhen the typeof operator returns “undefined” value, we know the variables to be detected has not been declared, but not yet initialized.

Null Type
  Null data type of the second type is only one value, the special value is null. From a logical point of view, a null value indicates a null object pointer, which is also used typeof operator returns a null value detected reason "object" when, as shown in the following example:  

var car = null; alert (typeof car); // “object”

If the variable is defined is prepared for future save the object, then the best this variable is initialized to null instead of other values. Thus, as long as the direct check for null can know whether the corresponding variables have been saved a reference to the object, as shown in the following example:

if (car! = null) {// perform certain operations on the car object} Indeed, undefined value is derived from a null value, and therefore a predetermined ECMA-262 test for equality to be returned to their true: alert (null == undefined); / / true

here, the equality operator located between the null and undefined (==) always returns true, however, to be noted that, this operator for comparison the object will be converted operands, although such a relationship between null and undefined, but their use is completely different. As described above, under no circumstances is not necessary to have the value of a variable is explicitly set to undefined, but not the same rules apply to null. In other words, as long as the variable is intended to hold the object has not really save the object, it should definitely make the variable holds nULL value. This can not only reflect NULL as an empty object pointer, but also help to further distinguish NULL and undefined.
  Thanks for reading, I hope to help everyone, thank you for your support of this site!  
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment