Js-based undefined-type sample code _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to determine the undefined type of JavaScript code. For more information, see The Code is as follows:


If (reValue = undefined ){
Alert ("undefined ");
}
The system found that the data could not be determined, and finally found the information to use the typeof method:
If (typeof (reValue) = "undefined "){
Alert ("undefined ");

}


Typeof returns a string, which may be "number", "string", "boolean", "object", "function", or "undefined"

3.4 Data Type
ECMAScript has five simple data types (also known as basic data types): Undefined, Null, Boolean, Number, and String. There is also a complex data type-Object, which is essentially composed of a group of unordered name-value pairs. ECMAScript does not support any mechanism for creating custom types, and all values will eventually be one of the above six data types. At first glance, it seems that only six data types are insufficient to represent all data. However, because the ECMAScript data type is dynamic, it is indeed unnecessary to define other data types.

3.4.1 typeof Operator
Since ECMAScript is loose, there is a need to detect the Data Type of a given variable-typeof is the operator responsible for providing this information. Using the typeof operator for a value may return the following string: Unknown "undefined" -- if this value is undefined; then "boolean" -- if this value is a boolean value; separator "string" -- if this value is a string;

24 Chapter 1 Basic Concepts
Limit "number" -- if this value is a value; limit "object" -- if this value is an object or null; limit "function" -- if this value is a function. Below are several examples of using the typeof OPERATOR:
Var message = "some string"; alert (typeof message); // "string" alert (typeof (message); // "string" alert (typeof 95 ); // "number"
TypeofExample01.htm
These examples show that the operand of the typeof operator can be a message or a numeric literal. Note that typeof is an operator rather than a function. Therefore, parentheses in this example are not required although they can be used. Sometimes, the typeof operator returns confusing but technically correct values. For example, if typeof null is called, "object" is returned because the special value null is considered as an empty object reference. Safari 5 and earlier versions, Chrome 7 and earlier versions will return "function" when calling the typeof operator for the regular expression, while other browsers will return "object" in this case ".

Technically speaking, a function is an object in ECMAScript, not a data type. However, functions do have some special attributes, so it is necessary to distinguish functions from other objects by using the typeof operator.

The Code is as follows:


Function test1 (){
Var message;
If (typeof (message) = "undefined ")
Alert ("variable value undefined ");
Else
Alert (message );
}
Var cc = test1;
Cc ();

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.