Original address :https://blog.csdn.net/oscar999/article/details/9353713

Before introducing the differences between the three , Let's take a look first JS  Data type of .

stay Java ,C In such a language , Before using a variable , You need to define this variable and specify its data type first , Integer. , String type ,....

But in js Unified use of variables defined in var , Or not var You can also use .

that js Is there a concept of data type in the ? Of course. , Use typeof You can 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 pop-up value of the above example is "string", From this we can see that , js There are also data types .
js The data types in are undefined,boolean,number,string,object etc. 5 Kind of , front 4 Species are primitive types , The first 5 This is a reference type .

What's the difference between a primitive type and a reference type ? The concept of reference is similar to that of other languages , It's an address . 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 neglect object This characteristic of type , This is a place that is often misused . Similar to the above obj The change of caused objCopy Changes .
In addition to the above 5 Outside of the middle type , There is another kind. “function” The type of .

<!--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>

undefined and null, NaN The difference between
With the introduction above , It's easy to put undefined Separate from the other two .

undefined It's the type of variable that's being judged , And the other two judgments are the values of the variables .

undefined It can be used to indicate the following situation

1. Represents an undeclared variable ,

2. Variables declared but not assigned ,

3. An object property that doesn't exist

null It's a special kind object , It means no value ;

NaN It's a special kind number , It means no value ;

Comparison symbol (== or ===)
Use == , If the types on both sides are different , js The engine will first convert them to the same type for value comparison ;

Use ===, There is no type conversion , Different types , Definitely not equal .

example
With the above knowledge , Let's look at some interesting but confusing examples :

<!--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>

hold var s Change to var s = null Let's see what happens ~~

In general , Yes js A variable s I'm used to using if(s!=null) , If s If there is no definition , Will report undefined Of js error , So the complete empty decision can be done in the following way :

if(typeof(s)!="undefined"&&s!=null)

( Reprint )Javascript The non empty judgment in undefined,null, NaN More articles about the difference between

  1. Javascript The non empty judgment in undefined,null, NaN The difference between

    JS data type Before introducing the differences between the three , Let's take a look first JS  Data type of . stay Java ,C In such a language , Before using a variable , You need to define this variable and specify its data type first , Integer. , String type ,.... however ...

  2. js Judge undefined type ,undefined,null,NaN The difference between

    js Judge undefined type Use today showModalDialog Open the page , On return value . When you click the close button on the open page or directly click close on the browser, the return value is undefined   So make your own smart judgment        ...

  3. [ Experience ] About Java The non empty judgment in

    When writing a project , There's a problem Suppose there is a control layer interface that is : @ResponseBody @RequestMapping(value = "test", method = Reques ...

  4. JavaScript in undefined,null,NaN The difference between

    1. Type analysis : js The data types in are undefined,boolean,number,string,object etc. 5 Kind of , front 4 Species are primitive types , The first 5 This is a reference type .var a1;var a2 = true;va ...

  5. js in undefined,null,NaN The difference between

    1. Type analysis : js The data types in are undefined,boolean,number,string,object etc. 5 Kind of , front 4 Species are primitive types , The first 5 This is a reference type .var a1;var a2 = true;va ...

  6. List and String Non empty judgment of

    1. If you want to judge list Is it empty , It can be judged that : if(null == list || list.size() ==0 ){ // Empty situation }else{ // Not empty } 2.list.isEmp ...

  7. C/C++ The pointer is not null

    Be sure to make a clear distinction C and C++ Of “ Null pointer constant ” It's not the same .C Standards don't guarantee NULL be equal to 0, So when we judge that the pointer is not null , Should use the if(p != NULL): because “ Context switch to bool value ” Unity of ,C++ You should use if(p) ...

  8. JAVAWEB servlet When verifying the login, make full non null judgment to prevent null login

    If we don't make a complete non empty judgment , So right. ""  Null values of this type result in direct login So we need to use the following string processing method to judge In this way, you can prevent null login Easy to confuse mistakes : The null value login here is ...

  9. String Non empty judgment of 、Integer Non empty judgment of 、list The size of , Non null judgment of objects

    1.String Non empty judgment of . StringUtils.isNotEmpty(String str); 2.Integer Non empty judgment of . null != Integer ; 3.list The size of . li ...

Random recommendation

  1. Give Way powershell Only one script can run at a time ( Examples of process mutex )

    powershell,mutex, Mutually exclusive , Processes are mutually exclusive , Scripts are mutually exclusive powershell Examples of script mutex , stay powershell In the category article , Statement original only . powershell The preacher Original article 2016-07- ...

  2. HttpClient(4.3.3) Instance to explain

    HttpClient It's powerful , It's really powerful . This example is based on v4.3.3 Written ,, The function is to simulate clock in and clock out after landing ,,, Use htmlparser Parsing returns html file About HttpClient Some of ...

  3. use DIV+CSS Cut multiple backgrounds and merge images CSS Sprites technology

    A long time ago on Internet sites and some js I've seen applications of this technology in plug-ins , I thought it was a lot of trouble , It's no use , I didn't go into it . I've been doing some front desk work recently , It involves a lot of div+css The problem of . I met this thing again , So I spent some time ...

  4. Linux Basic network construction experiment

    One . The goal of the experiment utilize 3 Virtual machines , build vmnet2 and vmnet3 Two host-only The Internet , Realize the interconnection of two networks Two . Experimental environment   Intranet Extranet gateway IP 192.168.0.10/24 202.3.4.1 ...

  5. [darknet] Look at the error results sight of wrong

    import os import numpy import cv2 bad_label_file = open("bad_valid.list",'r') names = [] f ...

  6. 5、 ... and 、regularized Linear regression exercises ( Reprint )

    Reprint link :http://www.cnblogs.com/tornadomeet/archive/2013/03/17/2964515.html Preface : This section is mainly about exercises regularization Application of item ...

  7. Java Theoretical hours section 6 . Homework after class .

    package Fuction; class Grandparent { public Grandparent() { System.out.println("GrandParent Cre ...

  8. c Language learning notes --- precompile

    Topic 3 : 1)       precompile Deal with all the comments , Replace... With a space , Will all #define Delete , And expand all the macro definitions , Processing conditional compilation instructions #if,#ifdef,#elif,#else,#endif Handle # ...

  9. Android The client and server are connected through DES Encryption Authentication

    Reprinted address :http://blog.csdn.net/spring21st/article/details/6730283 because Android Applications don't look like web Developing session Mechanism , So using PHPS ...

  10. Luogu P1352 A dance without a boss 【 Tree form DP/ Adjacency list + Chain forward star 】

    Title Description A university has N A clerk , The number is 1~N. There is a subordination between them , In other words, their relationship is like a tree rooted in the headmaster , The parent node is the direct superior of the child node . Now there's an anniversary party , Every time an employee is invited to a banquet, a certain happiness index will be increased Ri, ...