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, [ ], 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 |
new (operator)
return this (operator) var while with |
Note
new and this are not statements, but are included in this section for convenience.
break
function testBreak(x) {
var i = 0
while (i < 6) {
if (i == 3)
break
i++
}
return i*x
}
1. // comment text
2. /* multiple line comment text */
// This is a single-line comment.
/* This is a multiple-line comment. It can be of any length, and
you can put whatever you want here. */
i = 0
n = 0
while (i < 5) {
i++
if (i == 3)
continue
n += i
}
for ([initial-expression;] [condition;] [increment-expression]) {
statements
}
condition is evaluated on each pass through the loop. If this condition evaluates to true, the statements in statements are performed. This conditional test is optional. If omitted, the condition always evaluates to true.
increment-expression is generally used to update or increment the counter variable.
for (var i = 0; i < 9; i++) {
n += i
myfunc(n)
}
for (variable in object) {
statements }
object is the object for which the properties are iterated.
statements specifies the statements to execute for each property.
function dump_props(obj, obj_name) {
var result = ""
for (var i in obj) {
result += obj_name + "." + i + " = " + obj[i] + "<BR>"
}
result += "<HR>"
return result
}
To return a value, the function must have a return statement that specifies the value to return. You cannot nest a function statement in another statement or in itself.
In addition to defining functions as described here, you can also define Function objects, as described in "Function".
function name([param] [, param] [..., param]) {
statements }
param is the name of an argument to be passed to the function. A function can have up to 255 arguments.
//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 (condition) {
statements1 }
[else {
statements2}]
statements1 and statements2 can be any JavaScript statements, including further nested if statements. Multiple statements must be enclosed in braces.
if ( cipher_char == from_char ) {
result = result + to_char
x++ }
else
result = result + clear_char
Creating a user-defined object type requires two steps:
To define an object type, create a function for the object type that specifies its name, properties, and methods. An object can have a property that is itself another object. See the examples below.
You can always add a property to a previously defined object. For example, the statement car1.color = "black"
adds a property color to car1, and assigns it a value of "black". However, this does not affect any other objects. To add the new property to all objects of the same type, you must add the property to the definition of the car object type.
You can add a property to a previously defined object type by using the prototype property. This defines a property that is shared by all objects of the specified type, rather than by just one instance of the object. The following code adds a color property to all objects of type car, and then assigns a value to the color property of the object car1. For more information, see "prototype".
Car.prototype.color=null
car1.color="black"
birthday.description="The day you were born"
objectName = new objectType ( param1 [,param2] ...[,paramN] )
objectType is the object type. It must be a function that defines an object type.
function car(make, model, year) {Now you can create an object called mycar as follows:
this.make = make
this.model = model
this.year = year
}
mycar = new car("Eagle", "Talon TSi", 1993)This statement creates mycar and assigns it the specified values for its properties. Then the value of mycar.make is the string "Eagle", mycar.year is the integer 1993, and so on.
You can create any number of car objects by calls to new. For example,
kenscar = new car("Nissan", "300ZX", 1992)Example 2: object property that is itself another object. Suppose you define an object called person as follows:
function person(name, age, sex) {And then instantiate two new person objects as follows:
this.name = name
this.age = age
this.sex = sex
}
rand = new person("Rand McNally", 33, "M")Then you can rewrite the definition of car to include an owner property that takes a person object, as follows:
ken = new person("Ken Jones", 39, "M")
function car(make, model, year, owner) {To instantiate the new objects, you then use the following:
this.make = make;
this.model = model;
this.year = year;
this.owner = owner;
}
car1 = new car("Eagle", "Talon TSi", 1993, rand);Instead of passing a literal string or integer value when creating the new objects, the above statements pass the objects rand and ken as the parameters for the owners. To find out the name of the owner of car2, you can access the following property:
car2 = new car("Nissan", "300ZX", 1992, ken)
car2.owner.name
return expression
function square( x ) {
return x * x
}
this[.propertyName]
function validate(obj, lowval, hival) {You could call validate in each form element's onChange event handler, using this to pass it the form element, as in the following example:
if ((obj.value < lowval) || (obj.value > hival))
alert("Invalid Value!")
}
<B>Enter a number between 18 and 99:</B>
<INPUT TYPE = "text" NAME = "age" SIZE = 3
onChange="validate(this, 18, 99)">
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 necessary in functions if a global variable of the same name exists.
var varname [= value] [..., varname [= value] ]
value is the initial value of the variable and can be any legal expression.
var num_hits = 0, cust_no = 0
while (condition) {
statements
}
statements is a block of statements that are executed as long as the condition evaluates to true. Although not required, it is good practice to indent these statements from the beginning of the while statement.
n = 0Each iteration, the loop increments n and adds it to x. Therefore, x and n take on the following values:
x = 0
while( n < 3 ) {
n ++
x += n
}
n < 3
is no longer true, so the loop terminates.with (object){
statements
}
statements is any block of statements.
var a, x, y
var r=10
with (Math) {
a = PI * r * r
x = r * cos(PI)
y = r * sin(PI/2)
}