Basic knowledge of condition judgment in javascript
Source: Internet
Author: User
Javascript is a scripting language that can be run in a browser. It is a weak language (compared with C, C #, JAVA ), as long as it is a computer language, it will use conditional attention. JavaScript, as a "weak" language, is often confusing in terms of condition judgment, especially those who have experience programming in other strong languages, it is even more unreasonable! Don't make fun of experts and unconfused people. I have been depressed for a while. Write an example first:
Var s = "meizz ";
If (s & s = true)
{
Alert ("Is true ");
}
No matter whether the above Code runs correctly or not, I assigned the variable "s" a struct type, in the if condition's formula, the variable s is directly written as a judgment expression, if a variable of the strong language is used to determine whether its type is of the stable type or whether the value of this variable is equal to a certain string, the type of judgment (typeof) is not used in javascript) there is no value judgment (=), and it is so alone that it can be used as a condition judgment expression. Of course, this independent conditional expression is not only a variable of the numeric type, but also a variable of the numeric type, an object, or a function or method. This is exactly what this syntax is confusing.
I tested the statistics: In JavaScript, A string not empty "", a number not 0, an object not null, an object not undefined, a Boolean true, when it is used as an expression in the implicit expression, JavaScript can be regarded as true, whereas false ).
The reason is that the JS script does not have a strong variable type. You assign var s = "meizz" to the previous sentence, and you can even write s = function (){};, there is no type conversion in the middle, and these statements are incredible in strong languages, or even undo (this syntax is not supported at all), but it is so natural in JS scripts. The reason is that there is no forced variable type, and the variable type is dynamic. That is to say, the variable itself has no type, and the variable value has a type. Well, I will write an article about variable types.
Speaking of this, it is not difficult to understand if (s &&..... in the same sense, I can even function s () {} and then use if (s &&.... or var s = document. getElementById ("ObjectId"); if (s &&.... in this way, the syntax is correct in the JS script. For the judgment result, refer to the above statistical result.
Now let's discuss the running results of the above Code. The merge results of multiple conditions run only "with" and "or ", to "Non", because it is a one-dollar runtime, only for a single value, such as if (! S)... as for the case where there are more than one conditional, the conditional results only run with and or. And (&), or (| ). The conditional formula above: if (s & s = true) is the Synthesis Judgment of two conditional statements. As for the operation details (if a value is false, the value is false) or operation (if a value is true, the value is true, textbooks are much better than my pen. Now let's analyze this formula: this is an "and" operation. The first attention-type s, because its attention-type value is not "", in JS of course think it is equivalent to true, the second attention-type is s = true, obviously this is not equal, the value is false. Because it is an "and" operation, of course, the result of the entire conditional formula is false, so alert () is not run ().
Third, let me talk about some special conditions. In the absence of any declaration (var) and value assignment, directly using a variable to judge JS will throw an undefined variable error. For example:
If (ss) alert ("condition judgment result is true! ");
An error occurs when you run the code. Because the ss variable has never been declared assigned a value, this is not the same as the preceding null "" undefined. Where does this happen? First, write your own code and do not declare it. Second, when you directly use the ID to operate the controls that are not found in the webpage, such as if (InputId. value! = "")..., If there is no text box with id = "InputId" on the webpage or the text box is not loaded by the browser when the script is executed, an undefined variable error is thrown.
The solution is as follows: In the first case, use typeof to judge the result. If (typeof (ss) = "undefined") alert ("variable undefined"); in the second case, do not directly use ID to reference the object and use standard object reference. Example:
Var e = document. getElementById ("InputId"); // document. all. InputId is used in IE.
If (e & e. value! = "");//......
In this way, this error will not occur.
So some people will say that the above Code must be written in this way. Why not directly?
If (document. getElementById ("InputId"). value! = "");//....
Isn't this code more refined? Although the Code is simplified, the error is also returned. If the webpage does not contain this object or the object is not loaded during script execution, an error is returned. It turns out to be document. getElementById ("InputId") returns a null value, while null obviously does not have the value attribute, and I used e in the code. value is used to obtain the attribute, but no error is reported because another external attribute of the C series language in multiple conditional clauses: when multiple conditional clauses are combined for judgment, the first conditional criterion is first viewed, if the condition is met, the second criterion is not judged; that is, the second criterion is judged when the first criterion is not met, and so on until the end. If (e & e. value! = "") Is a combination of two operators. This is an "and" operation. If one operator is false, the entire value is false. The first implicit e returns null because it does not exist or is not loaded. In JS, null is equivalent to false, in this way, the result of the entire combination judgment is false without further judgment, so the system will not judge the sentence e. value. This is different from the B series language. In language B, if e and e. value! = "" Then is a combination of "and" after all the operators are computed. Therefore, this code is correct in JS. It is not necessarily correct if it is placed in VBS.
Well, there are not many items in your stomach, and the writing is too bad. I hope you can correct me a lot!
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.