Javascript Miscellaneous Statements with examples

This section describes the remaining three JavaScript statements with, a debugger, and  use strict.

With

With statement creates a temporary scope with the properties of an object as variables and then executes a statement within that scope. The with statement is used to temporarily extend the scope chain. It has the following syntax.

Syntax

        
With (object)
statement
        
    

This statement creates a temporary scope with the properties of the object

as variables and then executes a statement within that scope.

The with statement is forbidden in strict mode and should be considered deprecated in non-strict mode: avoid using it whenever possible.

JavaScript code that uses with is difficult to optimize and is likely to run significantly more slowly than the equivalent code written without the statement.

When we work with deeply nested object hierarchies we use statements.

In client-side JavaScript, for example, you may have to type expressions like this one to access elements of an HTML form.

document.forms[0].name.value

If you need to write expressions like this a number of times, you can use the with a statement to treat the properties of the form object like variables:

If we want to use this type of expression many times, we can use it with a statement for easier code use. 

Example

        
with(document.forms[0]) {
    // Access form elements directly here. For example:
name.value = "";
address.value = "";
email.value = "";
}
        
    

This reduces the amount of typing you have to do: you no longer need to prefix each form property name with document.forms[0]. That object is temporarily part of the scope chain and is automatically searched when JavaScript needs to resolve an identifier such as address. We can write the above example without the use of statements.

Example

        
let f = document.forms[0];
f.name.value = "";
f.address.value = "";
f.email.value = "";
        
    

debugger

The debugger statement in JavaScript is used for setting a breakpoint in the code. The code stops execution as soon as it encounters the debugger statement and calls the debugger function. 

The debugger statement normally does nothing. If, however, a debugger program is available and is running, then an implementation may (but is not required to) perform some kind of debugging action.

Syntax

        
debugger        
    

In practice, this statement acts as a breakpoint: execution of JavaScript code stops and you can use the debugger to print variables’ values, examine the call stack, and so on.

Suppose, for example, that you are getting an exception in your function f() because it is being called with an undefined argument, and you can’t figure out where this call is coming from. To help you in debugging this problem, you might alter f() so that it begins like this

Example

        
function sum (x) {
if (x === undefined) debugger;
  //code
}
        
    

In the above example, when function sum is called with no argument, execution will stop and we can use the debugger to inspect the call stack and find out which error gets.

We can also use the console.log method for debugging actions. If you’re using a web browser and have the developer tools console open, however, this statement will cause a breakpoint.

Example using console.log method

 

        
function sum(x,y){
  let x,y;
  let sum=0;
  sum=x+y;
  console.log(‘Addition function’);
  console.log(‘sum is ’, +sum);
}

        
    

From the above example, function sum returns the addition of two values. If there are any errors from the function then it’s displayed in the console section.

Use strict

In JavaScript, 'use strict'; states that the code should be executed in 'strict mode'. This makes it easier to write good and secure JS code.

To indicate this program is in the strict mode, we have used it at the top of the program. When you declare strict mode at the beginning of a program, it will have global scope and all the code in the program will execute in strict mode.

Syntax

        
‘use strict’        
    

We must use it strictly at the beginning of the program.

        
console.log("hello world");

// 'use strict' is ignored
// must be at the top
"use strict";

a=10; // does not throw an error
        
    

Some things are not allowed in the strict mode

i) Undeclared  variable is not allowed in the strict mode

        
'use strict';

x = 'hiii'; // throws an error
        
    

ii) Undeclared object is not allow in the strict mode

        
'use strict';

employee = {name: 'John', age: 30}; // throws an error
        
    

iii) Deleting an object is not allowed.

        
'use strict';
let person = {name: 'John'};
delete person; // throws an error
        
    

iv) Octal syntax is not allowed.

        
'use strict';
let x = 110; // throws an error
        
    

v) Duplicating a parameter name is not allowed.

        
"use strict";
function sum(x, x) { console.log('hello')};  // throws an error
sum();
        
    
Posted in

Ankit Patel

Leave a Comment

Your email address will not be published.

Scroll to Top