JS Data Type
Before introducing the differences between the three, let's take a look at the JS data types.
In a language such as Java and C, before using a variable, you must first define the variable and specify its data type, which is an integer, string type ,....
However, you can define variables in JS to use VaR in a uniform way, or you can also use it without using VAR.
Is there a data type concept in JS? Of course, you can use typeof to determine the Data Type of this variable:
<!--Add by oscar999--><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><TITLE> New Document </TITLE><META NAME="Author" CONTENT="oscar999"><script>s = "This is Test";alert(typeof(s));</script></HEAD><BODY></BODY></HTML>
The value popped up in the above example is "string". It can be seen that js also has a data type.
The data types in JS include undefined, Boolean, number, string, and object. The first four are original data types and the second are reference data types.
What is the difference between the original type and the reference type? References are similar to references in other languages. Let's take a look at this example.
<!--Add by oscar999--><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><TITLE> New Document </TITLE><META NAME="Author" CONTENT="oscar999"><script>var obj = new Object();var objCopy = obj;obj.att1 = "obj attribute";alert(objCopy.att1);</script></HEAD><BODY></BODY></HTML>
Don't ignore this feature of the object type. This is a frequent misuse. Similar to the above OBJ changes, the objcopy changes.
In addition to the above five types, there is also a "function" type.
<!--Add by oscar999--><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><TITLE> New Document </TITLE><META NAME="Author" CONTENT="oscar999"><script> function test() { alert("hello"); } alert(typeof(test));</script></HEAD><BODY></BODY></HTML>
Difference between undefined and null and Nan
With the above introduction, we can easily distinguish undefined from the other two.
Undefined determines the type of the variable, while the other two determine the value of the variable.
Undefined can be used to indicate the following conditions:
1. indicates an undeclared variable,
2. variables declared but not assigned values,
3. An existing Object Property
Null is a special object that indicates no value;
Nan is a special number that indicates no value;
Comparison operator (==or ==)
Use =. If the types on both sides are different, the JS engine first converts them to the same type for comparison;
If you use =, no type conversion is performed. If the types are different, they are definitely not equal.
Instance
With the above knowledge, let's look at some interesting but confusing examples below:
<!--Add by oscar999--><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><TITLE> New Document </TITLE><META NAME="Author" CONTENT="oscar999"><script> var s; alert(s==undefined); //true alert(s===undefined); //true alert(s==null); //true alert(s===null); //false alert(null==undefined); //true alert(null===undefined); //false</script></HEAD><BODY></BODY></HTML>
Change var s to VaR S = NULL and check the effect ~~
Generally, if (s! = NULL). If S is not defined, the undefined JS error will be reported. Therefore, you can use the following method to complete null determination:
if(typeof(s)!="undefined"&&s!=null)