YouTip LogoYouTip

Js Strict

JavaScript Strict Mode (use strict) | Tutorial

Tutorial -- Learning is not just about technology, but also about dreams!

JavaScript Tutorial

JavaScript Tutorial JavaScript Introduction JavaScript Usage JavaScript VScode JavaScript Run in Chrome JavaScript Output JavaScript Syntax JavaScript Statements JavaScript Comments JavaScript Variables JavaScript Data Types JavaScript Objects JavaScript Functions JavaScript Scope JavaScript Events JavaScript Strings JavaScript String Templates JavaScript Operators JavaScript Comparisons JavaScript If...Else Statements JavaScript switch Statement JavaScript for Loop JavaScript while Loop JavaScript break and continue Statements JavaScript typeof JavaScript Type Conversion JavaScript Regular Expressions JavaScript Errors – Throw, Try and Catch JavaScript Debugging JavaScript Hoisting JavaScript Strict Mode JavaScript Common Mistakes JavaScript Forms JavaScript Form Validation JavaScript Validation API JavaScript Reserved Keywords JavaScript this JavaScript let and const JavaScript JSON JavaScript void JavaScript Async Programming JavaScript Promise JavaScript async/await JavaScript Coding Conventions JavaScript Quiz

JS Functions

JavaScript Function Definition JavaScript Function Parameters JavaScript Function Invocation JavaScript Closures

JS Classes

JavaScript Classes JavaScript Class Inheritance JavaScript Static Methods

JS HTML DOM

DOM Introduction DOM HTML DOM CSS DOM Events DOM EventListener DOM Elements HTMLCollection Object NodeList Object

JS Advanced Tutorial

JavaScript Objects JavaScript prototype JavaScript Number Object JavaScript String Object JavaScript Date Object JavaScript Array Object JavaScript Boolean Object JavaScript Math Object JavaScript RegExp Object

JS Browser BOM

JavaScript Window JavaScript Window Screen JavaScript Window Location JavaScript Window History JavaScript Navigator JavaScript Popup Boxes JavaScript Timing Events JavaScript Cookie

JS Libraries

JavaScript Libraries JavaScript Testing jQuery JavaScript Testing Prototype

JS Examples

JavaScript Examples JavaScript Object Examples JavaScript Browser Object Examples JavaScript HTML DOM Examples JavaScript Summary

JS Reference

JavaScript Objects HTML DOM Objects JavaScript Async Programming JavaScript Static Methods

JavaScript Hoisting JavaScript Common Mistakes

JavaScript Strict Mode (use strict)

JavaScript strict mode runs under stricter conditions.


Using the "use strict" Directive

The "use strict" directive was introduced in JavaScript 1.8.5 (ECMAScript5).

It is not a statement, but a literal expression that is ignored in older versions of JavaScript.

The purpose of "use strict" is to specify that the code should be executed under strict conditions.

In strict mode, you cannot use undeclared variables.

Note Browsers that support strict mode: Internet Explorer 10+, Firefox 4+, Chrome 13+, Safari 5.1+, Opera 12+.

Strict Mode Declaration

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

In the example, we can press F12 (or click "Tools > More Tools > Developer Tools") in the browser to open debug mode and view error messages.

Debug Console

You can also right-click and select "Inspect" to view it, as demonstrated in the Gif below:

Inspect Element

Example

"use strict";
x = 3.14; // Error (x is not defined)

Try it yourself Β»

Example

"use strict";
myFunction();

function myFunction() {
  y = 3.14; // Error (y is not defined)
}

Try it yourself Β»

Declaring it inside a function makes it local (strict mode is only used within the function):

Example

x = 3.14; // No error
myFunction();

function myFunction() {
  "use strict";
  y = 3.14; // Error (y is not defined)
}

Try it yourself Β»


Why use strict mode:

  • Eliminates some unreasonable and sloppy aspects of JavaScript syntax, reducing some strange behaviors.
  • Eliminates some unsafe aspects of code execution, ensuring code runs securely.
  • Improves compiler efficiency and increases execution speed.
  • Paves the way for future new versions of JavaScript.

"Strict mode" reflects a more reasonable, safer, and stricter development direction for JavaScript. Mainstream browsers, including IE 10, already support it, and many large projects have begun to fully embrace it.

On the other hand, the same code may produce different results in "strict mode"; some statements that can run in "normal mode" will not run in "strict mode." Mastering this content helps to understand JavaScript more deeply and makes you a better programmer.


Restrictions of Strict Mode

Using undeclared variables is not allowed:

"use strict";
x = 3.14; // Error (x is not defined)

Try it yourself Β»

Note An object is also a variable.
"use strict";
x = {p1:10, p2:20}; // Error (x is not defined)

Try it yourself Β»

Deleting variables or objects is not allowed.

"use strict";
var x = 3.14;
delete x; // Error

Try it yourself Β»

Deleting functions is not allowed.

"use strict";
function x(p1, p2) {};
delete x; // Error
← Js MistakesPr Background Blend Mode β†’