Earlier, we saw that the comma operator can be used to combine a number of expressions into a single expression. JavaScript also has a way to combine a number of statements into a single statement, or statement block. This is done simply by enclosing any number of statements within curly braces. Thus, the following lines act as a single statement and can be used anywhere that JavaScript expects a single statement.
{ x =index.html Math.PI; cx = Math.cos(x); alert("cos(" + x + ") = " + cx); }
Note that although this statement block acts as a single statement, it does not end with a semicolon. The primitive statements within the block end in semicolons, but the block itself does not.
Combining expressions with the comma operator is an infrequently used technique in JavaScript. On the other hand, combining statements into larger statement blocks is extremely common. As we'll see in the following sections, a number of JavaScript statements themselves contain statements (just as expressions can contain other expressions); these statements are compound statements. Formal JavaScript syntax specifies that these compound statements contain a single substatement. Using statement blocks, you can place any number of statements within this single allowed substatement.
file: /Techref/language/JAVA/SCRIPT/definitive/ch05_02.htm, 4KB, , updated: 2019/10/14 15:00, local time: 2024/12/29 05:07,
52.15.42.61:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://linistepper.com/Techref/language/JAVA/SCRIPT/definitive/ch05_02.htm"> [Chapter 5] 5.2 Compound Statements</A> |
Did you find what you needed? |