How to judge the validity of variables in JavaScript

Source: Internet
Author: User
The avaScript language is not strictly designed. Sometimes it is easy to confuse people. For example, you can judge the effectiveness of this variable.
Here are several examples:
To understand why the above results are obtained, you must first understand the different meanings of undefined and null in JavaScript.
Here is a conclusion from instructor Ruan Yifeng's blog:
Null and undefined are basically synonymous, with only slight differences.
Null indicates "no object", that is, there should be no value.Typical usage:
(1) as a function parameter, it indicates that the function parameter is not an object.
(2) serves as the end point of the object prototype chain.
Undefined indicates "missing value", that is, there should be a value, but it is not defined yet.Typical usage:
(1) the variable is declared, but when no value is assigned, it is equal to undefined.
(2) when calling a function, the required parameter is not provided. This parameter is equal to undefined.
(3) the object does not have an attribute assigned a value. The value of this attribute is undefined.
(4) when the function does not return a value, undefined is returned by default.
In Java, the if condition must use a boolean expression. Therefore, when JavaScript is used, it is also customary to think so. In fact, when null, undefined, 0, "", and false in JavaScript are used as conditions for if, they are considered false.
Therefore, when determining a variable, you can use the following methods:
1. Accurately determine whether a variable is undefined
2. Accurately determine whether a variable is null
3. Determine whether a variable is null or undefined.
4. Determine whether the value is valid. You can directly use the variable name in the if expression. I often use it to determine whether the value of an input box is valid (note: if the variable value is 0, it will be treated as invalid)

For example:

If (! MyVar01) alert ("error occurred ");

// This code directly causes an exception because the variable myVar01 does not declare if ("undefined" = typeof myVar01) alert ("error occurred ");

// This will not cause exceptions.

Var myVar01; if (undefined = myVar01) alert ("error occurred ");

Let's take a look at the following simple example:
 
If (typeof (VAL1) = 'undefined '){
Var VAL1 = "now defined ";
} Else {
Alert ("already defined ");
}
 
Alert ("VAL1 =" + VAL1 );
 
 
You can determine whether a variable is defined by determining whether typeof (VAL1) = 'undefin. By the way, there is no block concept in javascript, so VAL1 is defined in the if statement and can still be accessed outside.
Note that if a var is defined in the function, the variable is the local variable of the function.
 
Let's look at the example below.
 
If (typeof (FUN1) = 'undefined '){
Alert ("now define the FUN1 ");
Function FUN1 (){
Alert ("this is FUN1 ");
   }
} Else {
Alert ("already defined ");
}
 
What do you think the output should be?
 
 
The correct answer should be alert ("already defined ");.
The function is different from the variable. For the keyword funtion, javascript is done during compilation, so the function is defined during execution.
In this way, the function can be used to determine whether to define a function.
If (typeof (FUNC1) = 'function ')
To check whether a function is declared. It may be useful for plug-ins.

// This code runs if ("undefined" = typeof myVar01) alert ("error occurred") correctly ");

// The code will also run correctly

Conclusion: We use the following method to ensure that if ("undefined" = typeof myVar01) alert ("error occurred") is foolproof ");

// The code will also run correctly

Of course, judging the effectiveness of data is far more than that, and there is also a null judgment, whether the number is too large.

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.