JavaScript tutorial:
with statement

 

The with statement establishes the default object for a statement.

Syntax

with (object)
   statement

The with statement syntax has these parts:

Part

Description

object

The new default object.

statement

The statement for which object is the default object. Can be a compound statement.

Example

The with statement is commonly used to shorten the amount of code that you have to write in certain situations. In the example that follows, notice the repeated use of Math:

function generateNumber()
{
with(Math)
{
var x, y ,z
x= cos(3 * PI) + sin (LN10)
y= tan(14 * E)
z=(pow(x,2) + pow(y,2)) * random()* 100;
}
return z;
}
document.write(generateNumber());

To run the code, paste it into JavaScript Editor, and click the Execute button.

See also: this statement