JS judge undefined type, undefined,null, the difference in detail _javascript skills

Source: Internet
Author: User

JS Judge undefined type

Today, you use ShowModalDialog to open the page and return the value. The return value is undefined when the open page clicks the Close button or closes the direct point on the browser
So be smart and judge.

var revalue=window.showmodaldialog ("", "", "");
if (revalue== undefined) {
Alert ("undefined");
}

Found that the judge does not come out, finally checked the data to use typeof

Method:
if (typeof (revalue) = = "undefined") {
Alert ("undefined");
}
typeof returns a string of six possible: "Number", "string", "Boolean", "Object", "function", "undefined"


The difference of Undefined,null,nan in JS

1. Type analysis:
The data type in JS has 5 kinds of undefined,boolean,number,string,object, the first 4 is the original type, and the 5th type is reference.
var A1;
var a2 = true;
var a3 = 1;
var a4 = "Hello";
var A5 = new Object ();
var a6 = null;
var a7 = NaN;
var a8 = undefined;
Alert (typeof a); Show "Undefined"
Alert (typeof A1); Show "Undefined"
Alert (typeof A2); Show "Boolean"
Alert (typeof A3); Show "Number"
Alert (typeof A4); Display "string"
Alert (typeof A5); Show "Object"
Alert (typeof A6); Show "Object"
Alert (typeof A7); Show "Number"
Alert (typeof A8); Show "Undefined"

As you can see from the code above, undefined values and undefined undefined,null is a special Object,nan is a special number.

2. Comparison operation
var A1; The value of A1 is undefined
var a2 = null;
var a3 = NaN;
alert (a1 = = A2); Display "true"
Alert (A1!= A2); Display "false"
alert (a1 = = A3); Display "false"
Alert (A1!= A3); Display "true"
alert (A2 = = A3); Display "false"
Alert (A2!= A3); Display "true"
Alert (a3 = = A3); Display "false"
Alert (A3!= A3); Display "true"

From the above code it can be concluded that (1) undefined is equal to NULL, (2) Nan is not equal to any value and is not equal to itself.

JavaScript Undefined Properties

Definitions and usage
The undefined property is used to store the undefined value of JavaScript.

Grammar
Undefined

Description
The undefined property cannot be enumerated using the for/in loop, nor can it be deleted with the delete operator.
Undefined is not a constant, you can set it to a different value.
Undefined is also returned when an attempt is to read an object property that does not exist.

Tips and comments
Tip: You can only use the = = operation to test whether a value is undefined, because the = = operator thinks the undefined value is equivalent to NULL.
Note: null means no value, and undefined represents an undeclared variable, or a variable that is declared but not assigned, or an object property that does not exist.

Instance
In this case, we will detect one of the two variables not defined:
<script type= "Text/javascript" >
var t1= ""
var T2
if (t1===undefined) {document.write ("T1 is undefined")}
if (t2===undefined) {document.write ("T2 is undefined")}
</script>

Output:
T2 is undefined

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.