JavaScript if...else...
JavaScript if else is a conditional judgment of JavaScript, similar to many other programming languages, if else is the most basic conditional judgment, and it is a basic function built in JavaScript. It can be called directly without additional installation. If else is designed according to the design The condition set by the teacher determines the execution item, and the condition can be set as an expression. If there are many conditions, you can use the else if to add the judgment item. The processing order of the judgment is based on the order of each condition. In addition to directly calling in general programs, it can also be called in JavaScript Loop . As a conditional judgment in the loop , JavaScript if else can be written in the following ways:
- if...
- if...else...
- if...else if...else...
JavaScript if condition judgment example
var Str = "A";
if(Str == "A"){
document.write("String is A.");
}
</script>
This can be regarded as the simplest conditional judgment. First prepare a variable Str and give the variable value A, and then use if to judge if the value of the variable Str is A, then output the result, here is A few small details should be paid attention to. First of all, the conditional judgment of if is inside the parentheses. Str == A must write a double equal sign here to be considered a conditional judgment. If it is written as the condition of Str=A, JavaScript will think that you want to change the variable Str. If the value is set to A, the if will completely consider the condition to meet the requirements, and directly execute the code in the if.
JavaScript if...else conditional judgment example
var Str = "B";
if(Str == "A"){
document.write("String is A.");
}else{
document.write("String is B.");
}
</script>
This example is an extension of the first example. The value of the variable Str is modified and the function of else is added. The first if condition determines that the variable Str is not A, so the code of else is executed. , Else does not care what your conditions are. As long as the previous judgments are not met, the code in else will be executed, so else there is no need to use parentheses to add conditions.
JavaScript if...else if...else... conditional judgment example
var Str = "B";
if(Str == "A"){
document.write("String is A.");
}else if(Str == "B"){
document.write("String is B.");
}else{
document.write("String is C.");
}
</script>
example is extended again, this time adding the judgment item of else if, please note that the writing of else if in JavaScript is separated by two letters, which is different from the writing of elseif in PHP. A designer who knows how to write PHP is easy to overlook when first contacting JavaScript. In this example, the result is already determined in the second round of judgment, that is, the else if, and the result is output directly, so the final else code will not be executed.
Remarks: Although the condition judgment of if else is quite easy to use, but you need to be careful not to be too complicated when setting the conditions, and try to avoid using a large number of if else if else conditions in the same program to avoid client-side processing consuming a lot of resources , Browsers need resources to process if else if else, but luckily, modern computer machines can handle a relatively large number of programs.
Post a Comment
0 Comments