Home > Article > Web Front-end > Troubles that JavaScript statements can not end with;_javascript tips
As a flexible scripting language, JavaScript's success is obvious to us. However, I have always been worried about the fact that JavaScript statements do not need to end with ";"! Apart from confusing statements and making it dependent on code formatting (line breaks), I haven't found any advantages to this feature. Is it because you can type one less ";"?! If you are used to the forced ";" in C#, C/C, etc., you will feel even more depressed.
At the same time, this feature is disastrous for JavaScript debugging. Is the following statement correct?
if ( results == 'AdvancedTimeSelect' )
{
this.DoAdvancedTimeSelect();
// . . .
}
else ( results && results.length > 0 )
{
// . . .
}
// . . .
This is me changing the "if"-"else" statement structure into "if"- Code written accidentally when using the "else if"-"else" structure (missing an if). If it is in a language such as C# that requires ";" as the end of a statement, this statement is an obvious grammatical error.
But the above code has no grammatical problems in JavaScript, and it can also run "normally". It's just that the normal side effects are too depressing. When this code is in a complete functional module, it brings endless confusion to debugging. Although another important reason is that I have poor eyesight, so I wrote it like this, but I still have to worry about the rule that JavaScript does not require statements to end with ";"!
The result of the above code is that if the first if condition is true, then the codes within the two () and the two {} will be executed. If it is not true, the code after else will be executed, including the ones enclosed by () and {}. Because JavaScript treats (results && results.length > 0) as a statement as a branch of else, then {} is an independent statement set.