The strange if-else, javascriptif-else in javascript

Source: Internet
Author: User

The strange if-else, javascriptif-else in javascript

Recently, my head is a bit dizzy. When I write the if-else code in javascript, I actually added conditions to else. The Code is as follows:

<script>var i=10;if(i<=10){    alert(111);}else(i>10){    alert(222);}</script>

Run this script in IE9 and chrome, and no js error is prompted. The execution result of the program is: The 111 dialog box is displayed first, and then the 222 dialog box is displayed. That is to say, if and else are both executed, and the traditional c/java is very different. In the project, I wrote a code similar to this code, and I did not see the problem after reading it for a long time. As an interpreted language, javascript does not have strict syntax requirements and compilation prompts like java/c. When writing javascript, you must be careful and standardized. Otherwise, you may feel very strange.

 

Why can the above javascript code be executed normally? Not clear. Thank you for your explanation and guidance.


Multiple questions about javascript if ---- else

If the date is normal, for example, month0 = 3
Else if (month0 = 1 | month0 = 3 | month0 = 5 | month0 = 7 | month0 = 8 | month0 = 10 | month0 = 12)
{
If (date0 <1 | date0> 31)
Alert ("the date is incorrect, or you must be an alien 2 ");
}
That is to say, if the else if branch is executed
Else
{
Alert ("normal ");
}
The else branch must be executed only when none of the if or else if values at the same level are met. However, when month0 is set to 3, else will not be executed.

Why cannot I use if () {} else if () {} In javascript (){}

It seems that your JavaScript function code is the display and hiding of the control layer.
The Code does not seem to have any problems. if -- else if can be used without any problems. I have used it before. We suggest you look at other places, for example, where the function is called;
If it is called in an event in the Form element, you can directly write the function name of the function. If you have any parameters, remember to give them to the parameter. If it is called in the href of the <A> label, the format is as follows: <a href = "javascript: che ()"> .. </a>

There is no problem with the above Code. look at other places.

Pass ~

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.