Tuesday 23 June 2020

26 - Web Designing Question - 06


Among the keywords below, which one is not a statement?

(A)    if

(B)    with

(C)    debugger

(D)   use strict


Here if, with and debugger are used in java script statement whereas “use strict” is not use in the statement.

use strict is a directive introduced in ECMAScript5. Directives are not statements because it does not include any language keywords. Also, it can appear only at the start of a script or at the start of a function body, before any real statement have appeared.

 

How to use the 'use strict' keyword in JavaScript?

1.      "use strict"; Defines that JavaScript code should be executed in "strict mode".

2.      It prevents you from using undeclared variables. It is like “option explicit” in VBScript.

3.      When we use “use strict” it is necessary to declare a variable before use.

4.      Strict mode is declared by adding "use strict"; to the beginning of a script or a function.

Declared at the beginning of a script, it has global scope

"use strict";

x = 3.14;       // This will cause an error because x is not declared

 

"use strict";

myFunction();


function myFunction() {

  y = 3.14;   // This will also cause an error because y is not declared

}

 

x = 3.14;       // This will not cause an error.

myFunction();


function myFunction() {

  "use strict";

  y = 3.14;   // This will cause an error

}

 

Deleting a variable (or object) is not allowed.

 

"use strict";

var x = 3.14;

delete x;                // This will cause an error

 

Deleting a function is not allowed.

 

"use strict";

function x(p1, p2) {};

delete x;                // This will cause an error 

 

Duplicating a parameter name is not allowed:

"use strict";

function x(p1, p1) {};   // This will cause an error

 

Octal numeric literals are not allowed:

"use strict";

var x = 010;             // This will cause an error

 

How to use the 'with' keyword in JavaScript?

The with keyword is used as a kind of shorthand for referencing an object's properties or methods.

The object specified as an argument to with becomes the default object for the duration of the block that follows. The properties and methods for the object can be used without naming the object.

Syntax

The syntax for with object is as follows −

with (object){

   properties used without the object name and dot

}

JavaScript's with statement was intended to provide shorthand for writing recurring accesses to objects.

So instead of writing

a.b.c.d.e.f.name=”ram”

a.b.c.d.e.f.age=25

You can write

with (a.b.c.d.e.f) {

    name=”ram”;

    age=25

}

 

JavaScript debugger Statement

1.      The debugger statement stops the execution of JavaScript, and calls (if available) the debugging function.

2.      Using the debugger statement has the same function as setting a breakpoint in the code.

3.      Normally, you activate debugging in your browser with the F12 key, and select "Console" in the debugger menu.

Note: If no debugging is available, the debugger statement has no effect.

Syntax

debugger;

 

var x = 15 * 5;

debugger;

document.getElementById("demo").innerHTML = x;

 

You all know about If statement. So here we are not discussing about if.

So Answer of this question is option (D) use strict 

No comments:

Post a Comment