Early versions of JavaScript were designed to be extremely forgiving. The philosophy was, "Don't stop the code from running, even if something looks wrong. Just make a guess and keep going." This is Non-Strict Mode, often called "sloppy mode."
The problem with this forgiveness is that it hides potential bugs. Your code might run, but it might be doing something completely different from what you intended. These are called silent errors.
Strict Mode was introduced in ES5 (2009) as a way to opt-in to a less forgiving, more secure, and more sensible version of the language.
The First Principle of Strict Mode: Its purpose is to change "sloppy" JavaScript that has silent errors or "bad syntax" into code that throws immediate, obvious errors. It helps you write better, safer code by forcing you to be more explicit.
You enable strict mode by putting the following string at the very top of a file or a function:
'use strict';
// File-level strict mode (Recommended)
'use-strict';
function myStrictFunction() {
// This whole function runs in strict mode
}
// ------------------------------------
function mySloppyFunction() {
// This function is in non-strict mode
function myInnerStrictFunction() {
'use strict';
// This inner function is in strict mode
}
}
Let's look at the most important changes strict mode introduces.