please dont rip this site

 

Statements

avaScript 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, [ ], 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

A statement that terminates the current while or for loop and transfers program control to the statement following the terminated loop.

Syntax

break

Implemented in

Navigator 2.0

Examples

The following function has a break statement that terminates the while loop when i is 3, and then returns the value 3 * x.

function testBreak(x) {
   var i = 0
   while (i < 6) {
      if (i == 3)
         break
      i++
   }
   return i*x
}


comment

Notations by the author to explain what a script does. Comments are ignored by the interpreter. JavaScript supports Java-style comments:

Syntax

1. // comment text
2. /* multiple line comment text */

Implemented in

Navigator 2.0

Examples

// 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. */


continue

A statement that 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, continue does not terminate the execution of the loop entirely: instead,

Syntax

continue

Implemented in

Navigator 2.0

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

A statement that creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a block of statements executed in the loop.

Syntax

for ([initial-expression;] [condition;] [increment-expression]) {
   statements
}

Arguments

initial-expression is a statement or variable declaration. It is typically used to initialize a counter variable. This expression may optionally declare new variables with the var keyword.

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.

statements is a block of statements that are executed as long as condition evaluates to true. This can be a single statement or multiple statements. Although not required, it is good practice to indent these statements from the beginning of the for statement.

Implemented in

Navigator 2.0

Examples

The following for statement starts by declaring the variable i and initializing it to zero. It checks that i is less than nine, 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

A statement that iterates a specified variable over all the properties of an object. For each distinct property, JavaScript executes the specified statements.

Syntax

for (variable in object) {
   statements }

Arguments

variable is the variable to iterate over every property.

object is the object for which the properties are iterated.

statements specifies the statements to execute for each property.

Implemented in

Navigator 2.0

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 = ""
   for (var i in obj) {
      result += obj_name + "." + i + " = " + obj[i] + "<BR>"
   }
   result += "<HR>"
   return result
}


function

A statement that declares a JavaScript function name with the specified parameters param. Acceptable parameters include strings, numbers, and objects.

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.

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.

In addition to defining functions as described here, you can also define Function objects, as described in "Function".

Syntax

function name([param] [, param] [..., param]) {
   statements }

Arguments

name is the function name.

param is the name of an argument to be passed to the function. A function can have up to 255 arguments.

Implemented in

Navigator 2.0

Examples

//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

A statement that executes a set of statements if a specified condition is true. If the condition is false, another set of statements can be executed.

Syntax

if (condition) {
   statements1 }
[else {
   statements2}]

Arguments

condition can be any JavaScript expression that evaluates to true or false. Parentheses are required around the condition. If condition evaluates to true, the statements in statements1 are executed.

statements1 and statements2 can be any JavaScript statements, including further nested if statements. Multiple statements must be enclosed in braces.

Implemented in

Navigator 2.0

Examples

if ( cipher_char == from_char ) {
   result = result + to_char
   x++ }
else
   result = result + clear_char


new

An operator that lets you create an instance of a user-defined object type or of one of the built-in object types Array, Boolean, Date, Function, Math, Number, or String.

Creating a user-defined object type requires two steps:

  1. Define the object type by writing a function.
  2. Create an instance of the object with new.

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"

Syntax

objectName = new objectType ( param1 [,param2] ...[,paramN] )

Arguments

objectName is the name of the new object instance.

objectType is the object type. It must be a function that defines an object type.

param1...paramN are the property values for the object. These properties are parameters defined for the objectType function.

Implemented in

Navigator 2.0

Examples

Example 1: object type and object instance. Suppose you want to create an object type for cars. You want this type of object to be called car, and you want it to have properties for make, model, and year. To do this, you would write the following function:

function car(make, model, year) {
   this.make = make
   this.model = model
   this.year = year
}

Now you can create an object called mycar as follows:

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) {
   this.name = name
   this.age = age
   this.sex = sex
}

And then instantiate two new person objects as follows:

rand = new person("Rand McNally", 33, "M")
ken = new person("Ken Jones", 39, "M")

Then you can rewrite the definition of car to include an owner property that takes a person object, as follows:

function car(make, model, year, owner) {
   this.make = make;
   this.model = model;
   this.year = year;
   this.owner = owner;
}

To instantiate the new objects, you then use the following:

car1 = new car("Eagle", "Talon TSi", 1993, rand);
car2 = new car("Nissan", "300ZX", 1992, ken)

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.owner.name


return

A statement that specifies the value to be returned by a function.

Syntax

return expression

Implemented in

Navigator 2.0

Examples

The following function returns the square of its argument, x, where x is a number.

function square( x ) {
   return x * x
}


this

A keyword that you can use to refer to the current object. In general, in a method this refers to the calling object.

Syntax

this[.propertyName]

Implemented in

Navigator 2.0

Examples

Suppose a function called validate validates an object's value property, given the object and the high and low values:

function validate(obj, lowval, hival) {
   if ((obj.value < lowval) || (obj.value > hival))
      alert("Invalid Value!")
}

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:

<B>Enter a number between 18 and 99:</B>
<INPUT TYPE = "text" NAME = "age" SIZE = 3
   onChange="validate(this, 18, 99)">


var

A statement that declares a variable, optionally initializing it to a value. 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 necessary in functions if a global variable of the same name exists.

Syntax

var varname [= value] [..., varname [= value] ]

Arguments

varname is the variable name. It can be any legal identifier.

value is the initial value of the variable and can be any legal expression.

Implemented in

Navigator 2.0

Examples

var num_hits = 0, cust_no = 0


while

A statement that creates a loop that evaluates an expression, and if it is true, executes a block of statements. The loop then repeats, as long as the specified condition is true.

Syntax

while (condition) {
   statements
}

Arguments

condition is evaluated before each pass through the loop. If this condition evaluates to true, the statements in the succeeding block are performed. When condition evaluates to false, execution continues with the statement following 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.

Implemented in

Navigator 2.0

Examples

The following while loop iterates as long as n is less than three.

n = 0
x = 0
while( n < 3 ) {
   n ++
   x += n
}

Each iteration, the loop increments n and adds it to x. Therefore, x and n take on the following values:

After completing the third pass, the condition n < 3 is no longer true, so the loop terminates.


with

A statement that establishes the default object for a set of statements. Within the set of statements, any property references that do not specify an object are assumed to be for the default object.

Syntax

with (object){
   statements
}

Arguments

object specifies the default object to use for the statements. The parentheses around object are required.

statements is any block of statements.

Implemented in

Navigator 2.0

Examples

The following with statement specifies that the Math object is the default object. The statements following the with statement refer to the PI property and the cos and sin methods, without specifying an object. JavaScript assumes the Math object for these references.

var a, x, y
var r=10
with (Math) {
   a = PI * r * r
   x = r * cos(PI)
   y = r * sin(PI/2)
}

file: /Techref/language/JAVA/SCRIPT/stmts.htm, 27KB, , updated: 2009/1/29 14:30, local time: 2024/11/12 11:34,
TOP NEW HELP FIND: 
3.138.67.97:LOG IN
©2024 PLEASE DON'T RIP! THIS SITE CLOSES OCT 28, 2024 SO LONG AND THANKS FOR ALL THE FISH!

 ©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?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://sxlist.com/techref/language/JAVA/SCRIPT/stmts.htm"> Statements </A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to sxlist.com!


Site supported by
sales, advertizing,
& kind contributors
just like you!

Please don't rip/copy
(here's why

Copies of the site on CD
are available at minimal cost.
 

Welcome to sxlist.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .