Here is a collection of JS in the null and undefined two hints of the difference between analysis, for each beginner should have no small use OH.
Generally known:
Null = = undefined
But:
Null!== undefined
So what's the difference between the two?
Null
This is an object, but is empty. Because it is an object, typeof null returns ' object '.
Null is a JavaScript reserved keyword.
Null is automatically converted to 0 when it participates in numeric operations, so the following expression evaluates to the correct value:
Expression: 123 + null result value: 123
Expression: 123 * Null result value: 0
Undefined
Undefined is a special property of the Global Object (window) whose value is undefined. But typeof undefined returned to ' undefined '.
Although undefined has a special meaning, it is indeed a property and is a property of the Global Object (window). Take a look at the following code:
Copy code code as follows:
Alert (' Undefined ' in window); Output: True
var anobj = {};
Alert (' undefined ' in anobj); Output: false
As you can see, undefined is a property of the Window object, but it is not a property of the Anobj object.
Note: Although undefined is a special-meaning attribute, it is not a reserved keyword for JavaScript.
Undefined participates in any numerical calculation, the result must be Nan.
By the way, Nan is another special property of the Global Object (window), infinity. These special properties are not javascript reserved keywords!
Improve undefined performance
When we use the undefined value in a program, we actually use the undefined property of the Window object.
Similarly, when we define a variable but do not give its initial value, for example:
var avalue;
At this point, JavaScript sets its initial value to a reference to the Window.undefined property when called precompiled.
So when we compare a variable or value to a undefined, it is actually compared to the undefined property of the Window object. During this comparison, JavaScript searches for the properties of the window object called ' undefined ', and then compares the reference pointers of the two operands to the same.
Because the property values of the Window object are very numerous, it takes time to search for the undefined property of the Window object in each comparison with the undefined. This can be a performance problem in a function that requires frequent comparisons with undefined. Therefore, in this case, we can define a local undefined variable to speed up the comparison of undefined. For example:
Copy code code as follows:
function Anyfunc ()
{
var undefined; Custom Local undefined variables
if (x = = undefined)//reference comparison on scope
while (y!= undefined)//scope reference comparison
};
Where the undefined local variable is defined, its initial value is a reference to the Window.undefined property value. The newly defined local undefined variable exists on the scope of the function. In subsequent comparisons, the JavaScript code was written in a way that was not changed, but was relatively fast. Because the number of variables on the scope is much less than the properties of the Window object, the speed of the search variable can be greatly improved.
This is why many front-end JS frameworks often have to define a local undefined variable for themselves!
Hope that through the introduction of this article, can bring you help
Null, object does not exist
var ii= document.getElementById ("id");
Alert (ii);
ID object does not exist on current page
Undefined
var i;
alert (i);
The declared variable is not initialized
alert (Document.oiji);
or object property, method does not exist
Empty
<asp Tutorial: TextBox id= "name" runat= "Server" width= "100px" text= "" ></asp:TextBox>
var IAB = document.getElementById ("name"). Value;
alert (IAB);
The value of Id=name is empty
JS null,undefined, String summary
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title>null,undefined, the memory occupancy of the string with the talk </title>
<style type= "Text/css Tutorial" >
body{font-size:12px;}
Pre{padding:5px;border:solid 2px #dfdfdf; color: #fff; Background-color: #666;}
</style>
<body>
<pre>
<strong> Summary:</strong>
The other thing to note is that NULL is an object type but it is very special and is stored in the stack stack. You can understand that it is a special form of object.
Also, in other object-oriented languages, strings are normally stored in the heap heap, but in JS this is stored in stack stacks
After that, suddenly think of a problem statement and the definition of the difference, think about the feeling itself is a thing, two kinds of statements. At this point, I suddenly thought that when declaring a variable,
Is it in the stack whether it occupies memory, (of course all the code will have code snippets, but in memory, but I'm not saying this), I mean whether undefined variables exist on the stack
Thought for a while I think that exists because the undefined type has only one value undefined, and when the declared variable is uninitialized, the default value is the literal amount of undefined.
Speaking of NULL, in fact it should have been in the heap but the JS heap ECMAScript implementation is wrong, and now is also accepted as a standard object placeholder
</pre>
<script language= "Web Effects" type= "Text/javascript" >
Alert (nan = = nan);//false
alert (undefined = = undefined);//true
alert (undefined = = null),//true, because the undefined is derived from null.
Alert (typeof null);
alert (undefined = = null); False
From the example above, you can write a function to determine whether the undefined is the following
var isundefined = function (param) {
return param = = undefined;
};
The other thing to note is that NULL is an object type but it is very special and is stored in the stack stack. You can understand that it is a special form of object.
Also, in other object-oriented languages, strings are normally stored in the heap heap, but in JS this is stored in stack stacks
</script>
</body>