Javascript - Statement with Statement


The with statement sets the scope for an object.

The syntax is as follows:

with (expression)

The following code:

var q =;
var h = location.hostname;
var u = location.href;

can be rewritten using the with statement as follows:

    var q = search.substring(1);
    var h = hostname;
    var u = href;

In the code, the with statement is used with the location object.

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