Trusted answers to developer questions

What is "use strict" in JavaScript?

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

use strict” is an expression stating that JavaScript code should be executed in “strict mode.” The expression was introduced in ECMAScript 5.

“Strict mode” is a more restricted version of JavaScript, where semantics are altered to make the code more resilient and secure. In “strict mode,” some silent errors are changed to throw errors and disable some of the more confusing or undefined features in JavaScript.

For example, you can not use undeclared variables in strict mode :

Press + to interact
"use strict"
a=1;

The above code throws an error while the code below executes :

Press + to interact
a=1;
console.log(a);

Another restriction can be seen in the example below where the code throws an error as the strict mode does not allow you to delete a variable. Try removing the "use strict" statement and it will work!

Press + to interact
"use strict"
a=1;
delete a;

Also, note that “use strict” has global scope when written at the beginning of the code while it will have a local scope if written inside a function body.

Here is an example of “use strict” in local scope:

Press + to interact
a=1;
//this will not throw an error as "use strict" is in local scope
function strictMode() {
"use strict"
alert("I am in strict mode");
}

RELATED TAGS

javascript
use strict
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?