Home  >  Article  >  Web Front-end  >  Conditional judgment in javascript_basic knowledge

Conditional judgment in javascript_basic knowledge

WBOY
WBOYOriginal
2016-05-16 19:18:291086browse

JavaScript is a scripting language that can run in the browser. It is a weak language (compared to C, C#, and JAVA). As long as it is a computer language, conditional judgments will be used, and JavaScript is a "weak" language. "Language, its conditional judgment is often confusing, especially for people who have programming experience in other strong languages, it is even more unreasonable! Don’t make fun of experts and unconfused people. I myself was depressed for a while. Let’s write an example first:

var s = "meizz";
if (s && s==true)
{
alert("Is true");
}

Regardless of whether the result of the above code is correct or not, I assigned the variable s a character type, and in the subsequent if conditional judgment, the variable s is directly written as a judgment expression Yes, if you deal with a character variable in a strong language, you need to either judge whether its type is a character or whether the value of the variable is equal to a certain string. But in JavaScript, there is no type judgment (typeof) and no value. Judgment (==) just sitting there can be used as a conditional judgment expression. Of course, this kind of conditional expression alone is not just a character variable, it can also be a numeric variable, an object, a function or a method, etc. That's what's so confusing about this syntax.

I tested and made statistics: in JavaScript, strings that are not empty "", numbers that are not 0, objects that are not null, object properties that are not undefined, and Boolean true can be used individually as When an expression in a judgment is considered true by JavaScript, otherwise it is considered false.

The reason is that JS scripts do not have strong variable types. If you assign a value in the first sentence var s = "meizz"; in the next sentence you can even write it immediately as s = function(){}; without any type conversion in the middle. These writing methods are inconceivable and even rebellious in strong languages ​​(this syntax is not supported at all), but they are so natural in JS scripts. The reason is that there is no mandatory variable type. The variable type is dynamic, which means that the variable itself has no type, but the value of the variable has a type. Haha, this involves other modules. Well, I will write another article to discuss the variable types.

Having said this, it is not difficult to understand the judgment expression of if (s &&.....For the same reason, I can even function s(){} and then use if (s && .... Or var s = document.getElementById("ObjectId"); if (s && .... This syntax is correct in JS scripts. As for the judgment results, you can refer to the statistical results above.

Now let’s discuss the results of the above few lines of code. The only combined operation of the results of multiple conditional judgment expressions is "and" and "or". As for "not", because it is a unary operator, it only For a single value, such as if (!s)... When there is more than one conditional expression, the result of the conditional expression is only AND and OR. The conditional expression above: if. (s && s==true) is a composite judgment of two conditional expressions. As for the AND operation (as long as one value is false, the value is false) or the operation (as long as one value is true, the value is true). I don’t know the details of these operations. Enough said, the textbook is much better than my pen. Now let’s analyze this judgment: This is an “AND” operation. The first judgment s, because its character value is not "", is certainly considered in JS. It is equivalent to true. The second judgment expression is s==true. Obviously this is not equal and the value is false. Because it is an "AND" operation, of course the result of the entire conditional judgment expression is false, so alert() will not be run.

Third, let me talk about some very special conditional judgments. Without any declaration (var) and assignment, JS will throw an undefined variable error if you directly use it to judge a variable. Come out. For example:

if (ss) alert("The conditional judgment result is true!");

An error will occur when running this code because this variable ss has never been declared and assigned a value. , this situation is not equivalent to the above-mentioned null "" undefined. Where does this situation usually occur? One is that you accidentally fail to declare it when you write the code yourself, and the other is to directly use the ID to operate the controls that are not in the web page. When, such as if (InputId.value!="") ... , if there is no text box with id="InputId" in the web page or the text box has not been loaded by the browser when executing this script operation , then an undefined variable error will be thrown.

The result is: use typeof to judge the first case.if (typeof(ss)=="undefined") alert("Variable is not defined"); In the second case, do not use ID to refer to the object directly but use standard object reference. Example:

var e = document.getElementById("InputId"); //IE used to use document.all.InputId
if (e && e.value!="");// ......

This way this kind of error will not happen.

So some people will say that the above code must be written like this, why not directly

if(document.getElementById("InputId").value != "") ;// ....

Isn’t this code more concise? Although the code has been streamlined, errors have also appeared. As long as there is no such object in the web page or the object has not been loaded when the script is executed, an error will be reported. It turns out that document.getElementById("InputId") returned the null value, and null obviously does not have a value attribute, and my code also used e.value to get the attribute but no error was reported. The reason is that the C series language is Another attribute in multiple conditional expressions: When judging multiple conditional expressions in combination, first look at the first conditional expression. If the condition is met, the second judgment expression will not be judged; that is, in the third judgment expression When a conditional judgment fails to meet the standard, the second judgment will be judged, and so on until the end. if (e && e.value!="") is a combined judgment of two judgments. This is an "AND" operation. As long as one judgment is false, the entire value is false. The first judgment expression 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 combined judgment can be obtained as false without subsequent judgments, so the system will no longer Let’s judge the following sentence e.value. This is different from the B series languages, so you need to pay special attention to it. In language B, a statement like if e and e.value!="" then is to first perform all the judgments and then perform the combined "AND" operation. So this code is correct in JS, but it may not be correct if it is placed in VBS.

Okay, I don’t have much in my stomach, and my writing is poor, so that’s all I’ve written. I hope everyone can correct me!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn