English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript basic tutorial

JavaScript object

JavaScript function

JS HTML DOM

JS browser BOM

AJAX basic tutorial

JavaScript reference manual

JavaScript Strict Mode (use strict)

ECMAScript 5The strict mode introduced in JavaScript isOpt-in One way to limit the variant of JavaScript.

Strict mode is not just a subset; it has semantics intentionally different from regular code.

Strict mode makes it easier to write 'safe' JavaScript.

Strict mode changes the previously accepted 'error syntax' to actual errors.

In strict mode, for example, you cannot use undeclared variables.

Invoke strict mode

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

To call strict mode for the entire script, enter the statement "use strict"; before any other declarations:

"use strict";
str = "嗨,我是严格模式脚本!";  // This will cause an error because str is not declared
Test and see‹/›

To call strict mode for a function, place the exact statement "use strict"; inside the function body, and place it before any other statements:

function myFunc() {
  // Function-level strict mode syntax
  "use strict";
  str = "大家好,我是严格模式函数!";  // This will result in an error
  return str;
}
Test and see‹/›

Strict mode can help you write cleaner code, such as preventing the use of undeclared variables.

The "use strict" directive can only be recognized at the beginning of a script or function.

Both single and double quote syntax are acceptable ('use strict'; or "use strict";).

Common restrictions in strict mode

As you know, in strict mode, all variables must be declared.

Assigning a value to an identifier that is not declared as a variable will cause a ReferenceError:

"use strict";
x = 5; // ReferenceError: x is not defined
Test and see‹/›

undeclared objects (objects are also variables) are not allowed:

"use strict";
coord = {x:10, y:20};   // ReferenceError: coord is not defined
Test and see‹/›

In strict mode, if you try to delete a variable, a syntax error will be thrown:

"use strict";
var msg = "Hello World";
delete msg;// SyntaxError
Test and see‹/›

Similarly, when you try to delete a function in strict mode, a syntax error will occur:

"use strict";
function sum(a, b) {
return a + b;
}
delete sum;// SyntaxError
Test and see‹/›

In strict mode, duplicate parameter names are not allowed:

"use strict";
function square(a, a) {  // SyntaxError
return a * a;
}
Test and see‹/›

In strict mode, writing to read-only properties is not allowed:

"use strict";
var person = {name: "Akash", age: 22};
Object.defineProperty(person, "gender", {value: "male", writable: false});
person.gender = "female";   // TypeError
Test and see‹/›

In strict mode, the with statement is not allowed:

"use strict";
with (Math) { x = sqrt(25);   // SyntaxError
Test and see‹/›

In strict mode, octal numbers are not allowed:

"use strict";
var x = 010;   // SyntaxError
Test and see‹/›

For security reasons,eval()It is not allowed to create variables within the scope that calls it:

"use strict";
eval("var x = 10);
;   // console.log(x);
Test and see‹/›

ReferenceError: x variable is not defined

"use strict";
The string "eval" cannot be used as an identifier (variable name):3codebox.com";// SyntaxError
Test and see‹/›

The string "arguments" cannot be used as an identifier (variable name):

"use strict";
var arguments = "oldtoolbag.com";// SyntaxError
Test and see‹/›

Paving the way for future ECMAScript versions

Future ECMAScript versions may introduce new syntax, ES5The strict mode in it applies some restrictions to simplify the transition.

It is easier to make some changes if the basis for prohibiting these changes in strict mode is removed.

The following identifier list becomes reserved keywords in strict mode:

  • implements

  • interface

  • let

  • package

  • private

  • protected

  • public

  • static

  • yield

"use strict";
var package = true;  // This will cause an error
Test and see‹/›