The conditional judgment in JavaScript

Source: Internet
Author: User
Tags expression string variable
Conditional JavaScript is a scripting language that can be run in a browser, a weak language (as opposed to C,c#,java), where a conditional judgment is used as long as it is a computer language, and JavaScript, as a "weak" language, is often puzzled by its conditional judgments, In particular, there are other strong language programming experience of people, even more unreasonable! Master and the people do not tease, I myself have been depressed for some time. Let's write an example:

var s = "Meizz";
if (S && s==true)
{
Alert ("is true");
}

Let's just say that the above code runs correctly or not, I give the variable s the character type, and in the next if condition judgment, we write the variable s as a judgment expression directly. If the variable in a strong language is either to determine whether its type is a character or to determine whether the value of the variable is equal to a string, but in JavaScript there is no type judgment (typeof) and no value judgment (= =) and is so lonely there, Can be used as a condition to judge an expression. Of course, this alone as a conditional expression is not just a character variable, can also be a numeric variable, can also be an object, can be a function or method, and so on. This is where the syntax is puzzling.

I tested the statistics: in JavaScript, a string that is not empty "", a number that is not 0, an object that is not NULL, an object property that is not undefined, a Boolean true, and JavaScript is considered true when it is used as an expression in a single form of judgment. (true), whereas false (false).

Because is the JS script does not have the strong variable type, your previous sentence assigns the value var s = "Meizz"; The last sentence you can even immediately write S = function () {}; , there is no type of conversion in the strong language is inconceivable, even treason (it does not support this syntax), but in the JS script is so natural. The reason is that there is no coercion variable type, the variable type is dynamic, that is, the variable itself is not a type, the value of the variable is of type. Oh, here to other modules to go, um, about the variable type I'll write another article to discuss it.

Speaking of this, it is not difficult to understand if (s && ...). In the same way, I can even function s () {} and then use the IF (S && ...).  or var s = document.getElementById ("ObjectId"); if (s && ..... This syntax in the JS script is correct, as far as the results of the judgment can refer to the above statistical results.

Now to discuss the results of the above code, the results of multiple conditional judgments are run only "with" and "or", to "non" because it is a unary run, only for a single value, such as if (!s) ... As far as the conditional type is more than one, the result of the conditional is only with and or running. With (&&), or (| | )。 The above sentence condition: if (S && s==true) is the synthesis judgment of two conditions. As for the operation (as long as a value of false is false) or operations (as long as a value is true) the details of these operations I do not say more, textbooks than my pen is much better. Now to analyze this judgment: this is a "and" operation. The first judgment type S, because its character value is not "", in JS, of course, it is the same as true, the second is s==true, obviously this is not equal, the value is false, because it is the "and" operation, of course, the entire conditional judgment result is false, so will not run alert ().

Third, I am here to say a few very special conditions to judge. In the absence of any declaration (Var) and assignment, directly take a variable to judge JS will throw a variable undefined error out. Like what:

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

Running this code will make an error. Since this variable SS has never been declared assignable, this is not equivalent to the null "undefined" above. Where does this usually happen? One is to write your own code inadvertently not declared, the second is the Web page does not operate the control directly with the ID to operate, such as if (inputid.value!= "") ..., if there is no id= "inputID" text box, or if the text box is not loaded by the browser when the script is executed, it throws an error that is not defined by the variable.

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

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

This will not make such a mistake.

So the code above is going to say that it has to be written, why not just

if (document.getElementById ("inputID"). Value!= "");

Isn't this code more concise? The city code is streamlined, but the error also comes. As long as there is no object in the Web page or the object is not loaded when the script executes, an error is given. The original is document.getElementById ("inputID") returned a null value, and NULL is obviously no value attribute, and my code also used E.value to fetch attributes but there is no error, The reason is that the C-series language has another foreign attribute in multiple conditions: when judging by a combination of conditional judgments, the first condition is seen, and if the condition is met, the second judgment is not judged; that is, when the first conditional judgment fails to meet the standard, the second judgment is judged. And so on until the end. if (e && e.value!= "") is a combination of two judgments, this is a "and" operation, as long as one of the judgements is false the entire value is false. The first judgment type E because does not exist or is not loaded to return null, and in JS Null is equal to false, so do not have to judge in the back of the whole combination of the result is false, so the system will not judge the back of the sentence e.value. This is different from the B-series language, especially to note. In B language if E and e.value!= "" "then such statements are the first of all the judgments are calculated after the final combination of the" and "operation. So this code in JS is correct, if put in the VBS is not necessarily correct.

Well, the belly of the goods are not much, writing and more rotten, write so much, hope everyone a lot of corrections!



Related Article

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.