with Statement in Javascript

Description

The with statement sets the scope within a particular object. The syntax is as follows:


with (expression) 
   statement;

Example

For the following code,


var qs = location.search.substring(1); 
var hostName = location.hostname;

The code above can be rewritten using the with statement:


with(location){ 
    var qs = search.substring(1); 
    var hostName = hostname; 
}

In strict mode, the with statement is not allowed and is considered a syntax error.





















Home »
  Javascript »
    Javascript Introduction »




Script Element
Syntax
Data Type
Operator
Statement
Array
Primitive Wrapper Types
Function
Object-Oriented
Date
DOM
JSON
Regular Expressions