JavaScript authority Guide NULL and undefined_jquery of learning notes

Source: Internet
Author: User
Tags numeric value
Copy Code code as follows:

<!doctype html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
Null and undefined</title> of <title>javascript
<body>
<script type= "Text/javascript" >
/**
Null keyword ********************
The keyword NULL in JavaScript is a special value that represents "no value". Null is often treated as a special value of an object type, that is, a value that represents a "no object".
Null is a unique value, distinct from all other values. If the value of a variable is null, it means that its value is not a valid object, array, number, string
And any of the Boolean values.
When NULL is used in a Boolean environment, it is automatically converted to false. When it is used in a digital environment, it is automatically converted to the number 0. When it is used in a string environment, it
is automatically converted to "null."
*/
var $null = null;
For Boolean environments
if ($null) {
document.write ("Do not convert when NULL is used for Boolean environments");
}else{
document.write ("When NULL is used in a Boolean environment, it is automatically converted to false");//This sentence will be output
}
For digital environments
if (0 + $null = = 0) {
document.write ("<br/> when NULL is used in a digital environment, it is automatically converted to 0");//This sentence will be output
}else{
document.write ("<br/> does not convert when NULL is used in digital environment");
}
For string environments
document.write ("<br/> for string Environment:" + $null);//null
Typeof:object of NULL
document.write ("<br/> $null data type:" +typeof $null);//object
/**
undefined******************
Undefined is a special value that is not a keyword in JavaScript. Here are a few things to note:
1. When you use a variable that has been declared but has not been assigned a value
2. When using an object property that does not exist
In all two of these cases, the return is undefined this value. The undefined value is different from null.
When an undefined value is used in a Boolean environment, it is automatically converted to false;
When it is used in a digital environment, it is automatically converted to Nan; (Nan's explanation: The Nan property is a special value that represents a Non-numeric value.) This property is used to indicate that a value is not a number.
When it is used in a string environment, it is automatically converted to "undefined";
*/
var $aaa//declared, but not initialized.
var $bbb = "Test";//declared, and initialized.
var $obj = new Object ();
document.write ("<br/> $aaa type:" +typeof $aaa + ", $aaa value:" + $aaa);//undefined, undefined
document.write ("<br/> $bbb type:" +typeof $bbb + ", $bbb value:" + $bbb);//string, test
document.write ("<br/> $obj a nonexistent property MyProp type:" +typeof $obj. myprop+ ", $obj. MyProp value:" + $obj. MyProp);// Undefined, undefined
document.write ("<br/> output the value of a variable not declared: + $ABC);//Run Error
/**
Undefined and null connection
Although the undefined and null values are different, the = = operator regards both as equal.
var obj = new Objec ();
Obj.prop = = NULL//return True
If you want to strictly distinguish between undefiend and NULL, you need to use the = = operator or typeof
var obj = new Objec ();
Obj.prop = = NULL//return FALSE
typeof Obj.prop//output undefined
typeof null//Output Object
*/
</script>
</body>
Related Article

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.