JavaScript syntax
JavaScript statements consist of keywords used with the appropriate syntax. A single statement may span multiple lines. Multiple statements may occur on a single line if each statement is separated by a semi-colon.
Syntax conventions: All keywords in syntax statements are in bold Words in italics represent user-defined names or statements Any portions enclosed in square brackets, ie [and], are optional {statements} indicates a block of statements, which can consist... OF A Single Statement or Multiple Statements Delimited by a curly braces.
The Following Statements Are Available in JavaScript:
break comment continue for for ... in function if ... else return var while with
Break Statement
THE BREAK Statement Terminates The Current While or for loop and transfers program program..................
Syntax
Break
Examples
.
Function func (x) {
VAR i = 0;
While (i <6) {
IF (i == 3)
Break;
i ;
}
Return i * x;
}
Comment stat
Comments are notaution by the author to explain what the script does, and they area ignored by the interpreter. JavaScript supports java-style comments:
Comments on a single line area (//). Comments That SPAN MULTIPLE LINES Are Preceded by A / * And Followed by a * /. Syntax1. // Comment Text
2. / * Multiple Line Comment Text * / Examples // this is a single-line comment.
/ * This is a multiple-line comment. It can be of any length, and
You can put whatver you want here. * /
continue statement The continue statement terminates execution of the block of statements in a while or for loop, and continues execution of the loop with the next iteration In contrast to the break statement, it does not terminate the execution of the loop entirely:. instead, In a while loop it jumps back to the condition. In a for loop it jumps to the update expression. Syntaxcontinue Examples The following example shows a while loop that has a continue statement that executes when the value of i is 3. Thus, n takes On The Values 1, 3, 7, And 12. i = 0;
n = 0;
While (i <5) {
i ;
IF (i == 3)
CONTINUE;
n = i;
}
For Statement a for loop Consists of Three Optional Expressions, Enclosed In Parenthese and Separated by SEMICOLONS, FOLLOWED BY A Block of Statements Executed in the loop. The Parts of The for Statement Are:
The initial expression, generally used to initialize a counter variable. This statement may optionally declare new variables with the var keyword. This expression is optional. The condition that is is evaluated on each pass through the loop. If this condition is true, the statements in the succeeding block are performed. This conditional test is optional. If omitted, then the condition always evaluates to true. An update expression generally used to update or increment the counter variable. This expression is optional. A block of statements that are executed as long as the condition is true This can be a single statement or multiple statements Although not required, it is good practice to indent these statements four spaces from the beginning of the for statement Syntaxfor ([initial expression];... [condition]; [UPDATE EXPRESSION]) {
Statements
}
initial expression = statement |. variable declartionExamples This simple for statement starts by declaring the variable i and initializing it to zero It checks that i is less than nine, and performs the two succeeding statements, and increments i by one after each pass through the loop FOR (var i = 0; i <9; i ) {
n = i;
myfunc (n);
}
For ... in Statement The for Statement Iteserts Variable Var over All The Properties Of Object Obj. for Each Distinct Property, IT Executes The Statements in Statements. SyntaxFor (var in obj) {
statements} Examples The following function takes as its argument an object and the object's name. It then iterates over all the object's properties and returns a string that lists the property names and their values. function dump_props (obj, obj_name) {
Var result = "", i = "";
For (iin obj)
Result = obj_name "." i "=" OBJ [i] "/ n";
Return Result;
}
function statement The function statement declares a JavaScript function name with the specified parameters param. To return a value, the function must have a return statement that specifies the value to return. You can not nest a function statement in another statement or in itself. All parameters are passed to functions, by value. in other words, the value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function. Syntaxfunction ([param] name [ , PARAM] [..., param]) {
STATEments}}}}}} // this function returns the Total Dollar Amount of Sales, When
// Given The Number of Units Sold of Products A, B, and C.
Function Calc_sales (units_a, units_b, units_c) {RETURN UNITS_A * 79 Units_B * 129 Units_C * 699
}
if ... else statement The if ... else statement is a conditional statement that executes the statements in statements if condition is true. In the optional else clause, it executes the statements in else statements if condition is false. These may be any JavaScript Statements, Including Further Nested if Statements. Syntaxif (CONDition) {
Statements
} [else {
Else Statements
}] Examplesif (copher_char == from_char) {
Result = result to_CHAR;
X
Else
Result = result clear_char;
return statement The return statement specifies the value to be returned by a function Syntaxreturn expression;. Examples The following simple function returns the square of its argument, x, where x is an number function square (x) {.
Return x * x;
}
var statement The var statement declares a variable varname, optionally initializing it to have value. The variable name varname can be any legal identifier, and value can be any legal expression. The scope of a variable is the current function or, for variables declared outside . a function, the current application Using var outside a function is optional; you can declare a variable by simply assigning it a value However, it is good style to use var, and it is neccessary in functions if there is a global variable of. the same name. So, in general, it is a good idea to always use var, but you should definitely use it when declaring a local variable in a function, to ensure that any global variable of the same name does not override it. Syntaxvar Varname [= value] [..., varname [= value]]
Examples var num_hits = 0, CUST_NO = 0
while statement The while statement is a loop that evaluates the expression condition, and if it is true, executes statements. It then repeats this process, as long as condition is true. When condition evaluates to false, execution continues with the next statement following the Statements. althoughments.......................
}
Examples The Following Simple While Loop Itereates As Long Asn N ITITEN Three. Each Iteration, IT Increments N Andds It To X. Therefore, x And N Take On The Following Values
After first pass: x = 1 and n = 1 after second pass: x = 2 and n = 3 after third pass: x = 3 And n = 6 after completion the third pass, the condition n <3 is no longer true, so The loop terminates. n = 0;
X = 0;
While (n <3) {
N ; x = n;
}
with statement The with statement establishes object as the default object for the statements. Any property references without an object are then assumed to be for object. Note that the parentheses are required around object. Syntaxwith (object) {
Statements
} Exampleswith (math) {
a = pi * r * r
X = r * cos (Theta)
Y = r * sin (Theta)
}