JS judge undefined type sample code _javascript tips

Source: Internet
Author: User
Copy Code code as follows:

if (revalue== undefined) {
Alert ("undefined");
Found that the judge does not come out, finally checked the data to use the TypeOf method:
if (typeof (revalue) = = "undefined") {
Alert ("undefined");


typeof returns a string of six possible: "Number", "string", "Boolean", "Object", "function", "undefined"

3.4 Data types
There are 5 simple data types (also known as basic data types) in ECMAScript: Undefined, Null, Boolean, number, and string. There are also 1 kinds of complex data types--object,object are essentially composed of a set of unordered name-value pairs. ECMAScript does not support any mechanism for creating custom types, and all values will eventually be one of the 6 data types mentioned above. At first glance, it seems that only 6 data types are not sufficient to represent all the data, but since the ECMAScript data type is dynamic, it is not necessary to redefine other data types.

3.4.1 typeof operator
Since ECMAScript is loosely typed, there is a need to have a means to detect the data type of a given variable--typeof is the operator responsible for providing this information. Using the typeof operator on a value may return one of the following strings:  "undefined"-if the value is undefined,  "boolean"-if the value is a Boolean,  "string"-if the value is a string;

24 3rd Chapter Basic Concepts
 "Number"-if the value is numeric,  "Object"-if the value is an object or null; "function"-if the value is a function. Here are a few examples of using the TypeOf operator:
var message = "some string"; Alert (typeof message); ' String ' Alert (typeof (Message)); "String" alert (typeof 95); "Number"
These examples show that the operands of the typeof operator can be either a variable (message) or a numeric literal. Note that TypeOf is an operator rather than a function, so parentheses in the example are available, but are not required. Sometimes, the typeof operator returns some confusing but technically correct value. For example, calling typeof null returns "Object" because the special value NULL is considered to be an empty object reference. Safari 5 and previous versions, Chrome 7, and previous versions return "function" when you call the TypeOf operator on a regular expression, and other browsers return "object" in this case.

From a technical standpoint, a function is an object in ECMAScript, not a data type. However, functions do have some special properties, so it is necessary to differentiate functions and other objects by typeof operators.
Copy Code code as follows:

function Test1 () {
VAR message;
if (typeof (message) = = "undefined")
Alert ("Variable value not defined");
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.